Web Design for Beginners

Free top tips for web designers



Using Echo in PHP

By James Middleton - Added 18th of August 2008

In 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 quotes


We 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



  • Use a double quote with strings
  • Always end your quote with the same quote type that you started with
  • If the copy within your echo contains a single quote, then start and end with a double quote. Alternatively, use a '' to mask every single quote that naturally appears within your copy, ie. echo 'This isn't a double quote';. If you don't, then the echo won't know where to end the quoted copy. The same applies to the use of the double quote, ie. echo "There was a pause, "What are you doing?", he said.";


Joining echo statements


You 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 & Links

Article written by James Middleton - www.webdesign-4-beginners.co.uk.

Information for Publishers

You are free to republish this article, provided you retain all hyperlinks as active in the above credits.

Add your own comments

* Must complete
*
*
Please do not use HTML codes within this field

(Max chars: 1000)
Characters remaining:
*
Please type the above code into the box below
(case sensitive):

Please Note: All comments are reviewed before going live.



Web Design for Beginners is ©Copyright 2008 Turning Turnip Web Design - All rights reserved.

Warning: mysql_close(): no MySQL-Link resource supplied in /homepages/38/d234135144/htdocs/webdesign-4-beginners/inc/bot-half.php on line 19