Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP Variables

PHP Variables


Variables are "containers" used to store information:

Example

Run Example »


Similar to algebra

x=5
y=6
z=x+y

In algebra, we use letters (such as x) and assign them values (such as 5).

From the expression above, z=x+y, we can calculate that the value of z is 11.

In PHP, these letters are calledVariables.

lamp Variables are containers used to store data.

PHP Variables

Similar to algebra, you can assign a value (x=5) or an expression (z=x+y) to a PHP variable.

Variables can have very short names (such as x and y) or more descriptive names (such as age, carname, totalvolume).

PHP variable rules:

lamp Both PHP statements and PHP variables are case-sensitive.

Creating (declaring) PHP variables

PHP has no command to declare a variable.

A variable is created when you first assign a value to it:

Example

Run Example »

In the execution of the statement above, the variable txt will hold the value Hello world!, and the variable x will hold the value 5.

**Note:** When you assign a text value to a variable, please add quotes on both sides of the text value.


PHP is a loosely typed language

In the example above, we notice that we do not need to declare the variable's data type to PHP.

PHP automatically converts the variable to the correct data type based on its value.

In strongly typed programming languages, we must declare (define) the variable's type and name before using it.


PHP Variable Scope

The scope of a variable is the part of the script where the variable can be referenced/used.

PHP has four different variable scopes:


Local and global scope

Variables defined outside all functions have global scope. Except inside functions, global variables can be accessed by any part of the script; to access a global variable inside a function, you need to use the global keyword.

Variables declared inside a PHP function are local variables, accessible only inside the function:

Example

Testing variables inside the function:

" ; echo "variable x is: $x" ; echo "
" ; echo "variable y is: $y" ; } myTest ( ) ; echo "

Testing variables outside the function:

" ; echo "variable x is: $x" ; echo "
" ; echo "variable y is: $y" ; ?>

In the example above, the myTest() function defines the $x and $y variables. The $x variable is declared outside the function, so it is a global variable, while the $y variable is declared inside the function, so it is a local variable.

When we call the myTest() function and output the values of the two variables, the function will output the value of the local variable $y, but cannot output the value of $x, because the $x variable is defined outside the function and cannot be used inside it. To access a global variable inside a function, you need to use the global keyword.

Then we output the values of the two variables outside the myTest() function; the function will output the value of the global variable $x, but cannot output the value of $y, because the $y variable is defined inside the function and is a local variable.

Note You can use the same variable names in different functions, because the variable names defined inside these functions are local variables, acting only within that function.

PHP global Keyword

The global keyword is used to access global variables inside a function.

To call a global variable defined outside a function inside the function, we need to add the global keyword before the variable in the function:

Example

Run Example »

PHP stores all global variables in an array named $GLOBALS[indexin the array ]. index The name of the saved variable. This array can be accessed inside the function, and can also be used directly to update global variables.

The example above can be written like this:

Example

Run Example »


Static Scope

When a function completes, all of its variables are normally deleted. However, sometimes you want a local variable not to be deleted.

To do this, use static keyword:

Example

Run Example »

Then, each time the function is called, the variable will retain the value it had when the function was called previously.

**Note:** This variable is still a local variable of the function.


Parameter Scope

Parameters are local variables whose values are passed to the function by the calling code.

Parameters are declared in the parameter list, as part of the function declaration:

Example

We will discuss it in the PHP Functions chapter in more detail.