Intro to Variables

by caiPHP
Wednesday, 27th July 2005

Note these points:

Here's an example of a variable being set:

<?php
$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:

<?php
$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:

<?php
$var=1;
?>

You can also add, subtract, and multiply variables!

<?php
$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 :)


© caiPHP.com
View the original article Intro to Variables