Intro to Variables

by caiPHP
http://www.caiphp.com/

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


Options:
Printer Friendly
Email Friend

About The Author:

© caiPHP.com

Developer Categories



Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
MySQL
PHP
Python
XML

Web Hosting

BlueHost Hosting - Unlimited Bandwidth, Unlimited Disk Space, Unlimited Domains for only $3.95!

Developer Documentation

Developer Tools



Search our Developer Tutorials
  The DevSyndicate Network