Variables

☰ Menu Content


Variables are for storing a values, such as text string "Hello World!" or the integer value 4.

A variable can then be reused repeatedly, instead of typing out the value over and over again.

PHP defines a variable with the following form:

 $variable_name = Value;

Don't forget the dollar sign ($)at the beginning.

It is similar to Algebra (parallel example)

a=5 , b=2 ,c=4

a + b = 5 + 2 = 7
a x c = 5 x 4 = 20

a,b and c in Algebra are like variables in Php.

A Quick Variable Example

In the example shown below variables ($hello, $a_number and $otherNumber) are given values by using an equal sign [ = ]

PHP Code:
<?php
    $hello = "Hello World!";
    $a_number = 4;
    $otherNumber = 8;
?>


PHP does not require variables to be declared before being initialized.

PHP Variable Naming Conventions

Few rules needed to follow when choosing a name for your PHP variables.

  • Must start with a letter or underscore "_".
  • It must not have a space.
  • It may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • More than one word should be separated with underscores. $my_variable or capatalized , $myVariable

Note
For more detail please see Php Manual at http://www.php.net

Php Tutorial Content (menu)