1. Computing

Discuss in my forum

A Quick PHP Variable Reference

A Guide to Using Variables in PHP

By , About.com Guide

A variable is used in programming to represent another value. In PHP variables all start with a dollar sign ($). They can be set to a string or a numeric value.
 <?php 
 $a = "Alexa";
 //This sets the variable $a to a string value (Alexa)
 $b = 6;
 //This sets the variable $b equal to a number value (6)
 ?> 
You will notice that string values are contained with quotations [""] while numeric values are not. Instaed of just a static number, you can also set a variable equal to an equation. Here are some examples:
 <?php 
 $a = 3 + 5;
 //This sets the variable $a equal to 8 or (3 + 5)
 $b = 6 + $a;
 //This sets the variable $b equal to 14, or (6 + $a)
 ?> 
We can combine values using a period [.] This will literally mash the values together. Here are some examples:
 <?php 
 $a = 1;
 $b = 2;
 $c = $a.$b;
 //The value of $c is now 12, because we smashed the 1 and 2 together 
 $d= $c + 1 ; 
 echo $d; 
 // This will echo 13, our newly created 12 plus 1 is 13
 $e = "Number";
 $f = $e.$d;
 echo $f;
 //This will echo the string Number13, a combination of $e and $f 
 ?> 
This is different than just echoing both variables as a new variable is actually created from the other variables.

Another thing you can do is make a variable from a string by using {} symbols. So, by using this method you could pass a string from one page to another, and use that string to pull the information from a variable of the same name. Here is an example:

 <?php 
 $a= "SomeVar"; 
 $SomeVar= "test"; 
 echo ${$a};
 ?> 
This will return "test", which is the value of $SomeVar because by using the {} symbols we are creating the variable from the string in $a.
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Learn PHP
  5. PHP Variable - PHP Variable Number - PHP Variable Text

©2013 About.com. All rights reserved.