PHP Variables


Variables are "containers" for storing information:

Examples

<?php $x=5; $y=6; $z=$x+$y; echo $z; ?>

Run Example »

Similar to algebra

x=5
y=6
z=x+y

In algebra, we use letters (like x), and assign values to them (like 5).

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

In PHP, these letters are calledvariables。

lamp Variables are containers for storing data.


PHP Variables

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

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

PHP Variable Rules:

  • Variables start with the $ sign, followed by the variable name
  • Variable names must start with a letter or an underscore character
  • Variable names can only contain letters, numbers, and underscores (A-z, 0-9, and _ )
  • Variable names cannot contain spaces
  • Variable names are case-sensitive ($y and $Y are two different variables)
lamp PHP statements and PHP variables are case-sensitive.

Creating (Declaring) PHP Variables

PHP has no command for declaring variables.

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

Example

<?php $txt="Hello world!"; $x=5; $y=10.5; ?>

Run Example »

In the execution of the above statements, the variabletxtwill hold the valueHello world!, and the variablexwill hold the value5。

Note:When you assign a text value to a variable, put quotes around the text value.


PHP is a loosely typed language

In the above example, we notice that there is no need to declare the data type of the variable 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 type and name of the variable before using it.


PHP Variable Scope

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

PHP has four different variable scopes:

  • local
  • global
  • static
  • parameter

Local and Global Scope

Variables defined outside all functions have global scope. Apart from 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 and can only be accessed inside the function:

Example

<?php
$x=5; // global variable

function myTest()
{
    $y=10; // local variable
    echo "<p>Test variables inside the function:<p>";
    echo "Variable x is:$x";
    echo "<br>";
    echo "Variable y is:$y";
}

myTest();

echo "<p>Test variables outside the function:<p>";
echo "Variable x is:$x";
echo "<br>";
echo "Variable y is:$y";
?>

In the above example, the myTest() function defines the $x and $y variables. The $x variable is declared outside the function, so it is a global variable , and 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 the function. If you want 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 script 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 in these functions are local variables and only apply within that function.

PHP global Keyword

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

To access a global variable defined outside a function from within a function, we need to add the global keyword before the variable inside the function:

Example

<?php $x=5; $y=10; function myTest() { global $x,$y; $y=$x+$y; } myTest(); echo $y; //Outputs 15 ?>

Run Example »

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

The above example can be written like this:

Example

<?php $x=5; $y=10; function myTest() { $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; } myTest(); echo $y; ?>

Run Example »


Static Scope

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

To do this, when you first declare the variable, usestaticthe keyword:

Example

<?php function myTest() { static $x=0; echo $x; $x++; echo PHP_EOL; //Newline character } myTest(); myTest(); myTest(); ?>

Run Example »

Then, each time the function is called, the variable will retain the value it had during the last call of the function.

Note:The variable is still a local variable of the function.


Parameter Scope

Parameters are local variables used to pass values to a function from the calling code.

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

Example

<?php function myTest($x) { echo $x; } myTest(5); ?>

We will, inPHP Functionschapter, discuss it in more detail.

Other Extensions