Helping ordinary people create extraordinary websites!

Introduction to PHP Variables

By Vardhan
2005-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:

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




Tutorial pages:
  • Intro to PHP Variables

 1 Votes

You might also want to check these out:


Leave a Comment on "Introduction to PHP Variables"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS