Learn About Echo in PHP
Learn About Echo in PHP
As you saw in the previous lesson, the PHP command echo is for outputting text to the web browser. Throughout your PHP career you will be using the echo command more than any other. To output a string, like we have done in previous lessons, use PHP echo. You can place either a string variable or you can use quotes, like we do below, to create a string that the echo function will output.
Learn PHP with All In One Tuts !;"; ?>
Learn PHP with All In One Tuts !
In the above example we output “Hello!” . The text we are outputting is being sent to the user in the form of a web page, so it is important that we use proper HTML syntax!
In our second echo statement we use echo to write a valid Header 5 HTML statement. To do this we simply put the <h5> at the beginning of the string and closed it at the end of the string. Just because you’re using PHP to make web pages does not mean you can forget about HTML syntax!
Careful when echoing quotes!
I love using PHP!"; // OK because we escaped the quotes!echo "
I love using PHP!"; // OK because we used an apostrophe ' echo "
I love using PHP!"; ?>
It is pretty cool that you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes! Echo uses quotes to define the beginning and end of the string, so you must use one of the following tactics if your string contains quotations:
- Don’t use quotes inside your string
- Escape your quotes that are within the string with a backslash. To escape a quote just place a backslash directly before the quotation mark, i.e. \”
- Use single quotes for quotes inside your string.
See our example below for the right and wrong use of echo:
If you want to output a string that includes quotations, either use an apostrophe ( ‘ ) or escape the quotations by placing a backslash in front of it ( \” ). The backslash will tell PHP that you want the quotation to be used within the string and NOT to be used to end echo’s string.
Echoing variables is very easy. The PHP developers put in some extra work to make the common task of echoing all variables nearly foolproof! No quotations are required, even if the variable does not hold a string. Below is the correct format for echoing a variable.
Hello All In One Tuts. My name is: 4a
echoing variables and text strings
You can also place variables inside of double-quoted strings (e.g. “string here and a $variable”). By putting a variable inside the quotes (” “) you are telling PHP that you want it to grab the string value of that variable and use it in the string. The example below shows an example of this cool feature.
"; echo "Hi, I'm Vikas. Who are you? $my_string
"; echo "Hi, I'm Vikas. Who are you? $my_string All In One Tuts"; ?>
Hello Vikas. My name is: All In One Tuts
Hi, I’m Vikas. Who are you? Hello Vikas. My name is:
Hi, I’m Vikas. Who are you? Hello Vikas. My name is: All In One Tuts
By placing variables inside a string you can save yourself some time and make your code easier to read, though it does take some getting used to. Remember to use double-quotes, single-quotes will not grab the value of the string. Single-quotes will just output the variable name to the string, like )$my_string), rather than (Hello Bob. My name is: ).
PHP echo is not a function
Echo is not a function, rather it is a language construct. When you use functions in PHP, they have a very particular form, which we will be going over later. For now, just know that echo is a special tool that you’ll come to know and love!