Home and Learn: PHP Programming Course


More PHP Variable Practice

 

In the previous section, you started to work with variables. 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:

<html>
<head>
<title>More on Variables</title>
</head>
<body>

<?php

print ("Basic Page");

?>

</body>
</html>

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

<?php

$first_number = 10;
print ($first_number);

?>

All the code does is to print the contents of the variable that we've called $first_number. 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!

<-- Back One Page | Move on to the Next Part -->

Back to the PHP Contents Page

 

Email us: enquiry at homeandlearn.co.uk