“Echoing” is simply PHP jargon for displaying something. In HTML to display some text, all you do is type it out- you don’t even need any tags. However in PHP you have to use the echo command, like so:
echo 'Hello World!';
Printed on the page you’ll see:
Hello World!
You use the echo command to display anything you want: HTML, JavaScript- basically any client-side language. Here are some examples:
echo '<b>Hello World!</b>';
echo '<script type="text/javascript">document.writeln("Hello World!");</script>';
echo '<img scr="http://www.google.com/logos/googlebeta.jpg" />';
Echoing quotes
If the string you’re echoing contains quotes, it adds a little complication to the whole thing. Look at this example:
echo 'It's cold in the Winter.';
This won’t work because the string you’re trying to echo contains a quote (in it’s). One way to get around this is to swap to double quotes for the echo expression:
echo "It's cold in the Winter.";
This works fine, and will display what you’d expect:
It's cold in the Winter.
However this isn’t the best of practice, not least because there will probably come a time when you need to use both single and double quotes inside of the echo expression. You’ve now run out of types of quotes to use, so you have to start escaping them with the backslash (\). You use the backslash in front of the quote you want to escape- it basically tells PHP that the quote isn’t part of the echo command itself. For example:
echo 'It\'s cold in the Winter.';
By adding the backslash before the quote in It’s, the quote is escaped and PHP knows that it isn’t part of the echo statement. Don’t worry- the backslash won’t be displayed on-page- it’s purely there as an instruction to PHP.
Echoing variables
If you remember back to the previous section where we looked at variables, I said that there are two types: textual and evaluative. Well the same principle applies here. In all the examples above we’ve been echoing simple text strings, and have therefore wrapped it in quotes. But what if you wanted to echo a variable?
$var = 'Hello World!'; echo '$var';
This won’t actually display Hello World!- instead this is what you’d see:
$var
This happens because the quotes tell PHP not to evaluate the string first. In other words, PHP doesn’t know that $var is actually a variable.
This is the correct way to do it:
$var = 'Hello World!'; echo $var;
and it’ll display what you actually want it to:
Hello World!
Mixed echoing
You probably think that the echo statement can either be textual or evaluative; quoted or not quoted. Well, you’d be wrong. Look at this example:
$age = 27; echo 'I am ' . $age . 'years old.';
This will display:
I am 27 years old.
Let’s have a look at what’s happening here. The I am part of the sentence is wrapped in quotes, since it’s the textual part of string. Because $age is a variable it has no quotes, however there should be a dot to separate the two types. Similarly when you want to return to textual for the years old. part of the sentence, you should use a dot again.
A note about variation
Like a lot of things in life, there are different ways to achieve the same outcome in PHP as well.
Look back at the very first example on this page where we were echoing Hello World!. Contrary to what I said, technically you don’t actually need the quotes- it will work without them. You may come across this in other peoples’ code, but it is extremely bad practice and it will likely become a problem in more advanced coding.
On a similar note, look again at the most recent example, “Mixed echoing”. There are actually other methods of separating the different parts of a string, sometimes using different punctuation besides a dot, and even not using a separator at all. However again, this isn’t particularly good practice either- you’ll probably come across problems later on if you get into the habit of doing this.