PHP Echo
This page teaches you how to use the Echo function.
Echo and print are 2 ways to send data to the web browser. Although both are almost identical, echo is used by most people and it is also one of the most popular coding statements you will use when coding PHP. The syntax of these functions calls for the data sent to the browser to be in quotes. Single or doube quotation marks will work with both echo and print. And the function, like all PHP functions, must end with a semi-colon and is not case sensitive.ECHO - Strings and Variables
In this example we are echoing a string and then we are echoing a variable.PHP Page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php echo "This string was processed with PHP and outputted to the browser."; ?> <?php $variable="This variable was processed with PHP and outputted to the browser."; echo $variable; ?> </body> </html> |
||
This is how it looks
|
This string was processed with PHP and outputted to the browser. This variable was processed with PHP and outputted to the browser. |
||
ECHO - concatenation
You can echo strings and variables together. This is called concatenation. Concatenation is good to learn because you'll need to do it when coding the output from PHP scripts but concatenation is also a concept that can help with coding mySQL statements. When coding the echo statement you surround the text with quotes and then separate the quoted text from the variables using a period. Most coders put a space before and after the separating period just to make sure it is visible to the eye. Two examples are below.PHP Page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php $customer="Dave"; echo "Hello" . $customer<br>; echo "Hello" . $customer . ", how are you?"; ?> </body> </html> |
||
This is how it looks
|
Hello Dave Hello Dave, how are you? |
||
ECHO - Echoing Quotes the wrong way
You can use either single or double quotation marks to define what the output of a PHP script will be. It will work fine but you will run into problems if the quotation type you use to define the output is used in the text you want to output.This is because PHP will process the quotes which you want printed out. In other words, if you use double quotation marks to surround the text you want outputted but you use a double quotation mark inside the text you want outputted then PHP will read the quotation mark from your text output as the end of the output. For example, the following code will give an error because you are using single quotation marks to define your text but you also have a single quotation mark inside your text. Since you have 3 quotation marks PHP won't know where the beginning and ending of the outted data is.
PHP Page
|
<?php echo 'Bill O'Reily'; ?> | ||
This is how it looks
|
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in phppage.php on line 2 |
||
ECHO - Echoing Quotes the right way
There are 2 ways to fix the quoting problem. The first is to use "opposite quotes" - surround your data with double quotes if it contains a single quote or surround it with single quotes if it contains a double quote. But a problem will still arise if you have both a single and double quotation mark in the text.This brings us to the second way. With this technique you "escape" the quotes you don't want PHP to process by putting a backslash before the quote. This means that PHP will treat these quotes as regular text characters that you want outputted and not as characters that have any special coding meaning. The first two examples below use "opposite quoting". The second two examples escape the quotes.
PHP Page
|
<html> <head> <title>HTML tutorial</title> </head> <body> <?php echo 'My friend said "goodbye" to me today.'; ?> <?php echo "Bill O'Reily"; ?> <?php echo "My friend said \"hello\" to me today."; ?> <?php echo 'Bill O\'Reily'; ?> </body> </html> |
||
This is how it looks
|
My friend said "goodbye" to me today. Bill O'Reily My friend said "hello" to me today. Bill O'Reily |
||
Bookmark this page: |
Rate this page: |
Comments:
| please post comments | ||
|
admin November 21, 2006 |
||











