Web Design for BeginnersFree top tips for web designers |
Using Echo in PHPBy James Middleton - Added 18th of August 2008In PHP, echo is a language construct which allows you to display text directly to your browser. It simple 'echoes out' what ever you associate with it. Try the following in your source editor, and then upload it to your server. As with all PHP, the code is purely server-side, so it won't work with your 'Preview-Mode' within your WYSIWYG application. <?php echo "Hello PHP people!" ?> You should see the following within your browser and nothing else: Hello PHP people! So whatever you put after the echo construct, within the double quotes should be the only thing on screen. Let's try the same use of echo again, but this time, using two lines: <?php echo "Hello PHP people!"; echo 'Is this another line?'; ?> Notes two things about the above PHP. Firstly, I seperated the two different echo statements using a ';'. This is very important with most situation within PHP. This let's the 'Parsing' process understand where you are adding a new construct such as another echo. Secondly, I have mixed the use of single and double quotes. In this scenario, using either will not have any kind of effect, aside echoing out the text. How does it look in the browser? Only one line appears! To add a line break, we must add a little HTML to our echo: <?php echo "Hello PHP people!<br>"; echo 'Is this another line?'; ?> The parsing process can generate text, HTML and any other language into your page. Why use PHP echo?Well, you have seen nothing yet! You can use echo to echo-out variable data: <?php $total = 5 + 5; echo "5 plus 5 equals $total"; ?> Although this tutorial is not focusing on the use of 'string' variables, I think that the above string '$total' is fairly self-explanitary. Try it out in your browser. Echoing Single and double quotesWe have seen that single and double quotes can be echoed out without a problem, but why not stick to one or the other? What is the difference? Now that we have added a string to our code, the choice of using a single or double quote is an important consideration. Example: <?php $total = 5 + 5; echo "5 plus 5 equals $total<br>"; echo '5 plus 5 equals $total'; ?> Result: 5 plus 5 equals 10 5 plus 5 equals $total Notes how the second echo using the single quote literally echoes-out the string name rather than it's associated variable content. Basic quote rules
Joining echo statementsYou can easily mix your echo statements contain single and double quotes by 'breaking' them apart and joining them together again: Example: <?php $total = 5 + 5; echo "5 plus 5 equals $total<br>".'don't you agree? "No!", you say?'; ?> You could also try: Example: <?php $total = 5 + 5; echo 'Look, a string within text! Total:'.$total.'! I bet you didn't expect that'; ?> Credits & LinksArticle written by James Middleton - www.webdesign-4-beginners.co.uk.Information for PublishersYou are free to republish this article, provided you retain all hyperlinks as active in the above credits.Add your own comments |
|
Web Design for Beginners is ©Copyright 2008 Turning Turnip Web Design - All rights reserved.
|