Php Variables

  • All variables in PHP are denoted with a leading dollar sign ($). The value of a variable is the value of its most recent assignment. Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right. Variables can, but.
  • PHP $ and $$ Variables The $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc. The $$var (double dollar) is a reference variable that stores the value of the $variable inside it. To understand the difference better, let's see some examples.
  • Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types −. Local variables; Function parameters; Global variables; Static variables. Global Variables. In contrast to local variables, a global variable can be accessed in any part of the program.

The scope of a variable is defined as its range in the program under which it can be accessed. In other words, 'The scope of a variable is the portion of the program within which it is defined and can be accessed.'

The rules of adding a PHP variable inside of any MySQL statement are plain and simple: Any variable that represents an SQL data literal, (or, to put it simply - an SQL string, or a number) MUST be added through a prepared statement. In the above example Create a variable.

PHP has three types of variable scopes:

Php Variables In Html

  1. Local variable
  2. Global variable
  3. Static variable

Local variable

The variables that are declared within a function are called local variables for that function. These local variables have their scope only in that particular function in which they are declared. This means that these variables cannot be accessed outside the function, as they have local scope.

A variable declaration outside the function with the same name is completely different from the variable declared inside the function. Let's understand the local variables with the help of an example:

Php Variables In Url

File: local_variable1.php


File: local_variable2.php


Global variable

The global variables are the variables that are declared outside the function. These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable. However, these variables can be directly accessed or used outside the function without any keyword. Therefore there is no need to use any keyword to access a global variable outside the function.

Let's understand the global variables with the help of an example:


File: global_variable1.php


Php Variables_order

Note: Without using the global keyword, if you try to access a global variable inside the function, it will generate an error that the variable is undefined.

Php variables in url


File: global_variable2.php


Using $GLOBALS instead of global

Another way to use the global variable inside the function is predefined $GLOBALS array.


File: global_variable3.php

Php variables tutorial


If two variables, local and global, have the same name, then the local variable has higher priority than the global variable inside the function.


File: global_variable2.php


Note: local variable has higher priority than the global variable.

Static variable

It is a feature of PHP to delete the variable, once it completes its execution and memory is freed. Sometimes we need to store a variable even after completion of function execution. Therefore, another important feature of variable scoping is static variable. We use the static keyword before the variable to define a variable, and this variable is called as static variable.

Static variables exist only in a local function, but it does not free its memory after the program execution leaves the scope. Understand it with the help of an example:



File: static_variable.php

Php Variables Across Pages


Php Variables Tutorial

You have to notice that $num1 regularly increments after each function call, whereas $num2 does not. This is why because $num1 is not a static variable, so it freed its memory after the execution of each function call.

Next TopicPHP $ and $$