This site is still under construction. Currently doing: drafting text.
Variables in PHP
posted on 17 Dec, 2005This article is a tutorial for beginners which covers the basics of using variables in PHP. This tutorial assumes almost no existing knowledge of php.
If you're new to php the first thing you are going to have to get your head around is variables. Variables seemed scary at first for me, but just take your time and once you learn all about variables and how to manipulate them everything else becomes easy.
So what is a variable? The analogy I was presented with is that a variable is like a container - all it has is a name (container) and a value (what's inside). An example of a variable in this sense could be a jar of jam. The name is "jar" and the value is "jam".
<?php
$message = "hello world";
?>
Above is the correct way to set a variable. You'll see there are two main parts: $name and "value".
$message is the name of the variable. The name of this variable is "message", and the $ character is used to indicate that the text in our script is a variable. Variable names in PHP cannopt contain spaces or special characters. Therefor the following variable names are okay:
- [*]$message
- $secretmessage
- $secret_message
- $scrt_msg
- $message_two
- $secretMessage
- $msg
But the following variable names are
not okay:
- $m355493
- $!!message!!
- $secret message
Variable names are case sensitive, so $secretmessage does not mean the same thing as $secretMessage.
The next main part of the statement was = "hello world";
If you know anything about php already you should know that commands all end in the semicolon character ( ; ) and defining a variable is a command so has to be ended the same way. The equals sign ( = ) just means that the things either side of it are equal - in this expression that's true.
So our statement:
$message = "hello world";
Translates to:
the variable $message has a value of "hello world".
A variable can have three types of values: a string, a number, or a boolean. A boolean is simply a TRUE or FALSE and this tutorial wont worry about that.
The variable in our "hello world" example is a string. A string is a collection of characters. When setting a string variable you must wrap the string in quotes ( " ) or apostraphes ( ' ).
A number variable is something like $x = 1; which can be used for algebraic type functions.
Anyway, here are a few examples of what you can do with number variables:
<?php
$x = 3;
$y = 2;
$a = $x + $y;
$b = $x * $y;
$c = $x - $y;
?>
$a will now have the value of $x added to $y, which is 3+2, which is 5.
$b will now have the value of $x multiplied by $y, which is 3x2, which is 6.
$c will now have the value of $x minus $y, which is 3-2, which is 1.
Now, to finish the tutorial, I'll give a few examples of using the echo() function. echo() is a function used in php to produce output - to write something on the page.
This:
echo "hello world";
Is the same as:
$message = "hello world";
echo $message;
This:
echo 5;
Is the same as:
$number = 5;
echo $number;
This example is invalid:
$message = "dave said "oh noes"";
Here is how it should be:
$message = "dave said "oh noes"";
The backslash ( \ ) before the quote lets the php know that the quote mark is a part of the string, not the end of it.
Okay I think I've pretty much covered the basics.. I hope this helps.