PHP Variables and Data Types

In the previous tutorial, we introduced PHP and now it's time to understand PHP variables. So, What is variable? In a computer program, variable is nothing but a storage area. Programmers put data into the variables to be used and manipulated in the programs.

In PHP, a variable is defined in the following way:

$variable_name = value;

Some quick example  of PHP variables:

$string_variable = "Hello world";
$integer_variable = 10;
$float_number = 2.5;

Must know facts about variables!

  •  Variable starts with a dollar sign which is followed by its name.
  •  Variable name is case sensitive which means that variable $number is not same to $Number.
  •  Declaring a variable is not necessary, you can initialize a variable anytime.
  •  It must contain alphanumeric characters and underscore.
  •  It may start with underscore or letter.
  •  Length of the variable name has no limit.

Variable Scope:

Scope of  a variable points at the zone in which it remains alive and available. In PHP, variables are divided into following categories.

  • Local variable
  • Global variable
  • Static variable

Local variable: Local variable has local reach where it is declare. If it is declared within a function then its accessibility is limited to that function only.

Global variable: Global variable is declared outside a function and can be accessed every part of the program. It is access within  a function using a global keyword before its name.

Local Variable Example

 <html>
      <head><title>Use local and global variable</title></head>
           <body>
              <?php
                 $a=5;
                  function test() {
                        global $a;  // access global variable using global keyword
                        $b = 5;      // local variable
                        $c = $a + $b;  // $c is local variable that store the result of addtion of $a and $b. 
                        echo $c; // output 10 
                           }
                          test();
              ?>
           </body>
</html>

Static Variable: A static variable is a variable that can retain its value through the programs life. 

Static Variable Example

 <html>
    <head><title> use of static variable</title></head>
      <body>
           <?php
               function test() {
                static $a=0;
                echo $a;
                   $a++;
                  }
              test();
                 test();
                 test();
             ?>
     </body>
 </html>

In above example, variable "a"  will still contain the value from the last time the function was called and stored information in "a".
Output of the above program is 012.

Data Types in PHP:

There are eight data types in PHP, which are used to construct PHP variables:

  1. Integers: Integers are whole numbers, without a decimal point such as 1,10,24.
  2. Doubles: Doubles are floating points numbers like 12.098.
  3. Strings: Strings are a sequence of character enclosed with single or double quotes such as "Hello, World".
  4. NULL: It is special type. Null variable has no value and point to nowhere in memory.
  5. Booleans: It has only two possible values either True or False.
  6. Arrays: It is a collection of values that store in a single variable.
  7. Objects: It is an instance of PHP classes.
  8. Resources: It is the special variable that holding a reference to an external resource such as database connection. 

Leave a Comment

Your email address will not be published. Required fields are marked *