More PHP Variable Practice

You outputted text to a page. In the next few sections, you’ll do some more work with variables, and learn how to do your sums with PHP.

But now that you can print text to a page, let’s try some numbers. Start with the basic PHP page again, and save your work as variables2.php:

<title>More on Variables</title>


print ("Basic Page");



We’ll now set up a variable and print it to the page. So change your code to this:


$first_number = 10;
print ($first_number);


All the code does is to print the contents of the variable that we’ve called:


Remember: if you’re printing direct text then you need quotation marks; if you’re printing a variable name then you leave the quotes out. To see why, run the first script above. Then change the print line to this:

print ("$first_number");
In other words, add double quotation marks around your variable name. Did it make a difference? What did you expect would print out? Now change the double quotes to single quotes. Run your script again. With double quotes, the number 10 still prints; with single quotes, you get the variable name!