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.
The PHP echo construct is used as follows:
<?php 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:
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.
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 \.
<?php 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 />"; ?>
If you are a newcomer to PHP, take your time and read over this tutorial, as echo is an important construct in PHP.