HOW TO OUTPUT TO SCREEN WITH PHP USING ECHO
PHP has a built-in way to display output to screen. You would use an ECHO statement to output html to screen.
See this simple example.
<?
echo "John Smith";
?>
When you run this, it will output John Smith to your screen. PHP can also output HTML code as in the following example:
<?
echo "<b>John Smith</b>";
?>
When you run this, it will output John Smith (bolded) to your screen. Look at the page source for this. It will show: <b>John Smith</b>
What happens when you echo with PHP is that PHP processes this echo statement and sends the output to the web server which in turn sends this output to your browser. Also note that you web browser can only read html (it reads text but in html format) and not PHP. It is the PHP processor on the web server that reads PHP statements and then outputs it as HTML to your browser which then displays it for you to read off your screen.
Points to remember when using the echo statement:
- The echo statement starts with the word echo.
- Strings must be surrounded with single or double quotes (a string is a combination of characters, text or numbers).
- Numbers on their own do not need to be enclosed with quotes.
- Echo statement must be terminated at end line with a semi-colon.
Further examples:
| Echo statement | Outputs |
| echo "John Smith"; | John Smith |
| echo 'John Smith'; | John Smith |
| echo 699; | 699 |
| echo "John Smith" | error, no terminating semi- colon |
| echo John Smith; | error, no enclosed quotes |
|