PHP Variables – Variable Scope, Static, Local And Global Variable


Objective

How to define variables in PHP?
What are the variables scope in PHP?

Variables are like a container which holds temporary information in your program and once execution completed the memory occupied by variables get cleaned automatically. Variables are used for asking and storing user input, storing calculation results and display on the browser, defining constant etc. You can define variables in PHP starting with the $ (dollar) symbol. Based on stored data in variables, PHP automatically defines it datatype. You can define PHP variables like this.

$str = "Hello world" //String Variables
$num=23434 //Integer
$dcml=334.23 //Decimal

MORE FACT ABOUT PHP VARIABLES

  1. Variables name always starts with the $ (dollar) sign.
    Example: $str, $num, $res
  2. A variable must start with a character or underscore (_) symbol. You cannot create a variable using number or special character.
    Example

    $num, $_num //Correct $8num //Incorrect
  3. A Variable can contain only alphabet, number and underscore. Any special characters are not allowed.
    Example: $str_1 //Correct $str*1 //Incorrect
  4. Variable names are case-sensitive.
    Example: $str and $Str are different variable
  5. You cannot use reserved PHP keyword for initialize variables.
    Example:
    $switch //Incorrect $function //Incorrect

LOOSELY TYPED

PHP is Loosely Typed language because it automatically decides variable data type based on stored value. For example, if you store a name in a variable it automatically makes that variable string and if you store a number inside it, it automatically makes that variable integer. For example.

$str = “Hello Jack” //Automatically makes $str variable string.
$num = 1234 //Automatically makes $num variable int.

If you will see in other type-safe programming languages like Java, C# or C++, you will notice you need to define data type while creating a variable.

PHP VARIABLE SCOPE

You can define a variable at any place inside PHP Script and this variable is available for use in programming based on its scope. There are 3 types of variable scope in PHP.


1. Local

A variable within a function is considered as Local Scope and it is available only for that function. You cannot access this variable outside of the function. Because this variable is limited with only a function so, you can use same name variables in a different function.

Example
<html>
 <head>
  <title>Variable Scope</title>
 </head>
 <body>

  <?php
  function checkscope()
  {
  	$str="Hello world"; //local Scope
  	echo $str; // Accessible
  }  
  checkscope();
  echo $str; //Not accessible
  ?>
</body>
</html>
Output
Hello world

2. Global

A variable outside a function is considered as Global Scope and it can be accessed anywhere in the script but not in function. You cannot access Global Scope variable inside a function directly. To do that you need to use global keyword.

Example
<html>
 <head>
  <title>Variable Scope</title>
 </head>
 <body>

  <?php
  $str="Hello world"; //Global Scope
  function checkscope()
  {
  	echo $str; //Not accessible
  }  
  echo $str;
  checkscope(); //Accessible
  ?>
</body>
</html>
Output
Hello world

You can access global scope variable using global keyword.

Example
<html>
 <head>
  <title>Variable Scope</title>
 </head>
 <body>

  <?php
  $str="Hello world"; //Global Scope
  function checkscope()
  {
  	global $str;
  	echo $str; // accessible
  }  
  checkscope(); //Accessible
  ?>
</body>
</html>
output
Hello World

$GLOBALS[INDEX] ARRAY


All the global variables are stored in $GLOBALS[index] array so you can access the global variable directly from this array.

<html>
 <head>
  <title>Variable Scope</title>
 </head>
 <body>

  <?php
  $num1=5; //Global Scope
  $num2=10; //Global Scope
  function checkscope()
  {
  	$result = $GLOBALS[num1] + $GLOBALS[num2];
  	echo "Add : ",$result;
  }  
  checkscope(); //Accessible
  ?>
</body>
</html>
Output
Add : 15

3. Static

A static variable doesn't get deleted after execution of the function and it remembers its stored value.

<html>
 <head>
  <title>Variable Scope</title>
 </head>
 <body>

  <?php
  function checkscope()
  {
  	static $num = 5;
  	$num++;
  	echo "num = ",$num; //Now $num=6
  	echo "<br>";
  }
    
  checkscope(); //Print num=6
  checkscope(); //Print num=7
  checkscope(); //Print num=8
  ?>
</body>
</html>
Output
num = 6
num = 7
num = 8

When each time function checkscope() gets executed, it doesn’t forget the last value of $num variable because $num variable is marked with the static keyword.

SUMMARY

In this chapter you learned how to define variables in PHP. You also learned the different type of variable scope like local, global and static variable. In the next chapter you will learn about DataTypes in PHP.

Back Next