Introduction to PHP Variables
By Vardhan2005-06-24
Intro to PHP Variables
Note these points:
- each php variable starts with a $ prefix
- each php variable must start with a letter (numbers aren't allowed)
Here's an example of a variable being set:
$myvariable="i am a variable!";
?>
This sets a variable called $myvariable. Its value is i am a variable!.
Now lets slap this variable on the screen:
$myvariable="i am a variable!";
echo($myvariable);
?>
This displays, or "echos" the $myvariable's value to the screen. Thus, the output would be: i am a variable!.
Variables can also contain numbers, and any other special characters. Heres an example:
$var=1;
?>
You can also add, subtract, and multiply variables!
$var=1;
$var2=8;
$sum=$var + $var2;
echo("$sum");
?>
This displays $sum (9) on the screen. The 4th line of the code is where the addition takes place. the + sign between the two variables tells PHP to add them.
If you replace the + with a * sign, it multiplies $var and $var2. If you replace the + with a - sign, it subtracts $var from $var2. If you replace the + with a / sign, it divides $var and $var2.
Enjoy :)
Tutorial Pages:
» Intro to PHP Variables
