PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Variables and Assignment Operations

This section describes what is a variable, variable names prefixed with $, 'set' and 'unset' states of a variable, assigning data to variables, var_dump($var) to print variable information.

What is a variable? A variable is an identifier for a piece of data stored in memory during the program execution. In PHP, variables are used under these basic rules:

1. A variable name in PHP must be prefixed with a "$" sign. Perl has a similar rule for scale variables.

2. Variable names are case sensitive. So $address and $Address refer to two different variables.

3. Variables in PHP are typeless. Therefor there is no variable type declaration statements in PHP. Any variable can be used as the identifier for any data type.

4. A variable has 2 states: "set" and "unset". A variable is in the "set" state, if a piece of data has been assigned to it. A variable is in the "unset" state, if there is no data assigned to it.

5. Assignment operations (coded by the assignment operator, =) can be used to assign data to variables.

6. The unset($var) function can be used to remove the assigned data from the given variable.

7. Variables can used in any operations like data literals in PHP source code. If a variable with data assigned is used in an operation, the assigned data value will be used for the operation. If a variable with no data assigned is used in an operation, NULL will be used for the operation.

8. There are some special characters that you should not use them as part of a variable name, ' ', '$', '[', ']', '{', '}', ...

PHP has a nice built-in function, var_dump($var), that prints out detailed information about the specified variable.

To show you some of variable rules mentioned above, I wrote the following PHP script, VariableTest.php:

<?php # VariableTest.php
# Copyright (c) 2005 by Dr. Herong Yang. http://www.herongyang.com/
# 
   print "\nVariable assignments:\n";
   
   $u; # Not assigning to any data
   print "\n   \$u: "; var_dump($u);

   $b = TRUE; # Assigning a boolean
   print "\n   \$b: "; var_dump($b);

   $i = 777; # Assigning an integer
   print "\n   \$i: "; var_dump($i);
   
   $f = 3.14; # Assigning a float
   print "\n   \$f: "; var_dump($f);

   $s = "Hello Herong"; # Assigning a string
   print "\n   \$s: "; var_dump($s);

   $a = array("Jan"=>31,"Feb"=>28); # Assigning an array
   print "\n   \$a: "; var_dump($a);

   $o = new DateTime(); # Assigning an object
   print "\n   \$o: "; var_dump($o);

   $r = opendir("."); # Assigning a resource
   print "\n   \$r: "; var_dump($r);

   $n = NULL; # Assigning NULL
   print "\n   \$n: "; var_dump($n);
?>

If you run this sample script, you should get:

Variable assignments:

   $u: NULL

   $b: bool(true)

   $i: int(777)

   $f: float(3.14)

   $s: string(12) "Hello Herong"

   $a: array(3) {
  ["Jan"]=>
  int(31)
  ["Feb"]=>
  int(28)
  ["Mar"]=>
  int(31)
}

   $o: object(DateTime)#1 (0) {
}

   $r: resource(4) of type (stream)

   $n: NULL

Last update: 2005.

Sections in This Chapter

Variables and Assignment Operations

References and Variables

Variable Variable Name - Name Variables with Expressions

Constant and define() Function

Dr. Herong Yang, updated in 2009
Variables and Assignment Operations