PHP Variables

Variables are "containers" for storing information:

Create (declare) PHP variables

PHP does not have a command to declare variables.

The variable is created the first time you assign it to it:


$txt = "Hello PHP!";
$a = 2;
$b = 5.5;

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

Comment: When you assign a text value to a variable, put quotation marks around the text value.

PHP is a weakly typed language

In the example above, we noticed that it was not necessary to declare the data type of the variable to PHP.

PHP will automatically convert the variable to the correct data type based on the value of the variable.

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

PHP variable scope

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

PHP has four different variable scopes:

  • local
  • global
  • static
  • parameter

Similar to algebra

x = 5
y = 6
z = x + y

In algebra, we use a letter (such as x) and assign a value (such as 5).

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

In PHP, these letters are called variables .

variable is a container for storing data.

PHP variables

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

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

PHP variable rules:

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

Global and Local Scope

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

Variables declared inside PHP functions are local variables and can only be accessed inside the function:


=5// Global variable

function myTest()
$y=10// Localvariable
echo "<p>Test variable:<p>";
"variable x for: $x";
"variable y for: $y";


"<p>Test function variable:<p>";
"variable x for: $x";
"variable y for: $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 The $ y variable is declared inside the function so it is a local variable.

When we call myTest () function and output the values of two variables, The function will output the value of the local variable $ y, but it 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 in 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 it cannot output the value of $ y, because the $ y variable is defined in the function and belongs to the local variable.

You can use the same variable name in different functions, because the variable names defined in these functions are local variables and only apply to that function.

PHP global keywords

The global keyword is used to access global variables within functions.

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



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

PHP stores all global variables in an array named $ GLOBALS [ index ]. index holds the name of the variable. This array can be accessed inside the function or used to update global variables directly.

The above example can be written like this:


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

Static scope

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

To do this, use the static keyword the first time you declare a variable:


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

Then, each time the function is called, the variable will retain the value from the previous time the function was called.

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

Parameter scope

Parameters are local variables that pass values to functions by calling code.

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


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

We will discuss it in more detail in the PHP functions section.