Open phpvariables.php and save it as phptextvariables.php. Currently the code is a mess. Put a couple of blank lines after the last echo line and paste this in:
echo "<p>Hello $name, how are you?</p>";
echo "<p class=\"blue\">Hello world</p>";
echo "<p>I understand you are $months months old</p>";
Compare the old code and the new code. They both do exactly the same thing. Upload and try to understand:
- the PHP interpreter looks through text and when it sees a $ it knows there is a variable name after it
- it inserts the value there instead of showing it as text
The new code is better because it creates all of the variables in one block and then uses fewer echo statements. You could even use just one echo statement to do all three paragraphs perhaps like this:
echo "<p>Hello $name how are you?</p>
<p class=\"blue\">Hello world</p>
<p>I understand you are $months months old</p>";
Make yours like that. All three result in the same HTML so do it whatever way you want.
Escaping and braces
A couple of warnings though. First is that if you use a $ inside the text and it is not a variable PHP might get confused. So you have to escape $ signs just like quotation marks. The second warning is that sometimes you will not want spaces around the variable and PHP might miss it. You therefore enclose the variable in braces so that PHP can see where it starts and ends.
This code illustrates both of those points so paste it in under the existing lines:
$amountInDollars=5;
$time="4";
echo "<p>The amount is $$amountInDollars. The time is $timepm.</p>";
The first $ is intended to be an actual currency symbol to be displayed to the user. On older PHP servers the $ will be seen as the start of a variable. Save and upload and you may see an error about an undeclared variable (some versions of PHP will not be confused but some will so if you do not see an error don't worry). Escape the first $ with a backslash so that it looks like this and will work on all servers:
echo "<p>The amount is \$$amountInDollars. The time is $timepm.</p>";
The second $ is the start of a variable so is not escaped.
The second variable name ($time) has the letters pm which are not supposed to be part of the variable name but do need to come straight after the name without a space so that the time appears as 4pm not 4 pm. PHP assumes the variable name was timepm. Add curly brackets (braces) around the actual variable name to make it clearer for the server:
echo "<p>The amount is \$$amountInDollars. The time is {$time}pm.</p>";
The page should now work.
Putting braces around any variable does no harm so use them if you are not sure. Put braces around the first variable. It will not change the execution of the code but it might make it easier for you to read the code as now it is harder to confuse the two dollars.
As braces have this special meaning in PHP you may need to escape them if you want to use them as text.
Don't worry this could be the most annoying part of PHP so it should get better from now.