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:
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 $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!
TIP: We recommend you use single quotes for your direct text, and NOT double quotes - there's fewer hassles if you do!
No comments:
Post a Comment