PHP Echo Tutorial

One of the most common commands in PHP is the echo command. It is a means of sending output such as strings or other data to the user. We simply pass the data that we want echo and it will output it to the user for us.

Note carefully that the echo command is not a function and is actually a language construct.

We look at the PHP echo command in detail in the tutorial below.

Using echo to output in PHP

The PHP echo construct is used as follows:

  echo 'Whatever you want to show on the screen!!';
  $myString = '<br />I love Tutorial Arena';
  echo $myString;
  echo "$myString because it shows me this!";

The above code will give:

Whatever you want to show on the screen!!
I love Tutorial Arena
I love Tutorial Arena because it shows me this!

Remember, if you don't plan to output any variables using your echo command you should use single quotes to enclose your output. This is because PHP parses everything within double-quotes looking for variables to output. Thus, if you aren't echoing a variable, you should use single quotes with your echo command to make your script run faster.

You can also echo a variable directly (without using any quotes) as shown in the example above.

Note that we also output an HTML break tag with our variable. This is to ensure that is displays in a separate line on our screen. After all, we are viewing it with a browser so we need to ensure that our browser displays it exactly how we want.

Handling Quotes in PHP

As we can see, quotes are use to enclose whatever we are outputting with the echo command. So what happens if we want to output a quote? We have to use an escape sequence. An escape sequence tells the PHP interpreter that the next character is one that we want to use literally, that is, not a part of the PHP code. The escape sequence that is used with PHP is the backslash \.

  echo 'That\'s all there is to it<br />';
  echo 'This is how we print a backslash \\<br />';
  echo 'If we use a " it does not affect the single quote pair<br />';
  echo "And vice-versa ' as well<br />";


That's all there is to it
This is how we print a backslash \
If we use a " it does not affect the single quote pair
And vice-versa ' as well

If you are a newcomer to PHP, take your time and read over this tutorial, as echo is an important construct in PHP.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...