Assiging a value to a variable works from right to left. The value on the right will be assigned to the variable name on the left. Let's look at each one:
$variable = 10; - This simply assigns the value of 10 to the variable name $variable. If we now printed out the variable, it would return 10. If we used the variable in a function it would be handled as the value 10. Assigning 10 to a variable is just a way to give 10 a name for us to work with.
$variable = 'This is a variable'; - This assigns a string to the variable. This variable would now be equal to This is a variable. It allows us to easily work with the string, using a variable to represent it.
$variable_two = "$variable containing a variable"; - notice i used double quotes to contain the string this time. I did this because we want to assign the value of $variable to $variable_two. This will interpret the value of $variable and assign it, with the other parts of the string, to $variable_two. $variable_two would now contain "This is a variable containing a variable". If i were to use single quotes:
$variable_two would contain "$variable containing a variable". This is called interpolation. If you wish the value of a variable to be interpreted, it must be contained within double quotes, if it isn't, PHP will not interpret it and will use the actual string within the quotes.
$variable_three = $variable_two; - We can assign a variable to another variable easily. We do not need to contain it within any quotes. $variable_three will now be a copy of $variable_two. Referring to interpolation, what would happen if we did:
The first example would be the same as without quotes, they both work the same, it is just good coding practice to miss out the quotes if they're not necessary. The second example would not interpret the variable value and $variable_three would contain simply "$variable_two". Get to grips with interpolation.
Look in the example above:
What's the deal with that // or bit? That's a comment. You can leave little notes in your PHP coding, explain what is happening so others can understand your coding more easily. One line comments are of the format:
// This is a comment
Anything after // and on the same line, will not be parsed by PHP, PHP will know it is a comment and not interpret it as a PHP command. We can also use:
that can span multiple lines*/
This comment can span multiple lines, /* signifies the start of the comment, */ signifies the end of the comment. Any text between these two symbols will not be parsed by PHP.
# This is a comment
This can only be on one line also.
Now that we know how to use variables, let's write a page that uses them.
Firstly we open PHP mode and assign our variables their values. We then close PHP mode and print out the html. When we get to the title, we open php mode, echo the title variable, which contains "Hello World" and then clsoe PHP mode. We then print out more HTML, open PHP mode and echo the string containing my name, close PHP mode and print out the rest of the HTML. Easy eh?
In the above example, when we want to print out a variable name we do <?php echo $title; ?> . There is a shortcut for this, PHP is clever that way.
<?=$name?> is just a short way to do <?php echo $name; ?> . Remember the equal (=) sign, it's alot easier to use this method when printing out many variable values.
Note:
If you're a Perl lover trying to learn PHP, you may be surprised with how it behaves. If you submit a form containing:
The data submitted through this form input will automatically be available to you in the variable $username. If you set a cooke named "a_cookie", this will be automatically available to you in $a_cookie. If you send the query string:
The variable $section will be available to you automatically, and will contain "mail". This is a nifty feature of PHP, but some people question it security-wise. You can turn "register global variables" off in the PHP configuration file if you wish, to increase security. If you do:
But don't worry about that yet. Just thought i'd mention it.
That just about wraps it up for variables, let's go on and look at arrays.