HomePHP Page 3 - Programming PHP: A Beginner`s Guide
Variables - PHP
PHP has been taking the web by storm because of its power and versatility. If you'd like to add PHP-based applications to your web site, keep reading. This series of articles will teach you the language from scratch.
The best way to think of a variable is to think of a box. You store things in them. You can put stuff in them, take things out, move them around, and so forth. If you are an angry UPS guy who has some computer parts we just ordered, you can slam it on the floor and kick it a few times while glaring at all of the employees. Then you can politely ask for the signature and storm out (you know who you are!).
Variables in PHP (and in programming languages in general) store data. Most programming languages are a real pain in the butt and make you define what type of data is being stored. Not PHP. In Soviet Russia, PHP tells you what type of data is in the variable!
(Note: A special thanks to the great world-renowned comedian Yakov Schmirnoff).
Here is how you work with variables:
<html>
<body>
<?php
$the_greatest = "James Payne";
echo $the_greatest;
?>
</body>
</html>
The above code store the string (or text) James Payne in a variable named $the_greatest. We then used echo and told it to print the value of $the_greatest. Which would result in:
James Payne
A note about naming variables. They must begin with either a letter or an underscore(_) and can only contain letters, numbers, and underscores. Spaces are not allowed. If you wish to use a two or more word name, use either of these methods: $string_name or $StringName. And lastly, a $ must precede the name of the variable, to let PHP know you are using a variable.