PHP variables

PHP variables are containers for storing data.

all variables in php start with a $ sign symbol.

PHP naming rules for variables

rules for php variables

  • a variable starts with $ sign, followed by the name of the variable
  • variable names are case-sensitive, $x and $x are two different variables
  • a variable name cannot start with a number (0-9)
  • a variable name must start with a letter or the underscore( _ ) character
  • a variable name can only contain alpha-numeric characters and underscores


        legal  variable names                        |          illegal   variable   names

                          $ram                                                     $ra m   
                          $ram8                                                    $8ram
                          $t_t_t                                                   $*this
                          $_ram                                                    $-ram

look in this example how declare variables and store value.

                   $x = 30;         // x variable hold 30 value
                   $x = 50;
                   $name = "this is string value within double quotes";// name hold sandeep kumar
                   $sum  = 40+50;                 // sum hold 90 value
                   $_digit_total = 4040+5050;   
                            echo "x value = $x";
                            echo "x value = $x";
                            echo "name   is =  $name";
                            echo "sum   = $sum ";
                            echo "digit total = $_digit_total";

PHP is a loosely typed language

in the strongly typed programming language (c c++ java), the programmer must declare the name and type of the variable before using it.

in the loosely typed language variable type only depended on the variable value.

php automatically converts the variable to the correct data type, depending on its value.

in this example describe, what is a loosely typed language:

                $xx = 3050;       // xx variable is now integer type
                                 var_dump( $xx );
                $xx = "stanford uni";   // xx variable is now string type
                                 var_dump( $xx );
                $xx = 34034.3434;   // xx variable is now float type
                                var_dump( $xx );
    class  bill{}                                
                $xx = new  bill(); // xx  is object type
                                var_dump( $xx );