PHP Variables

Variables are containers for storing information:

Example

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

Running Instance

Similar to algebra

x=5
y=6
z=x+y

In algebra, we use letters (such as x) to store values (such as 5).

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

In PHP, these three letters are calledVariable.

Note:Consider variables as containers for storing data.

PHP Variables

Like algebra, PHP variables can be used to store values (x=5) and expressions (z=x+y).

Variable names can be very short (such as x and y), or more descriptive (such as carname, total_volume).

PHP Variable Rules:

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

Note:PHP variable names are case-sensitive!

Creating PHP Variables

PHP does not have a command to create variables.

Variables are created when they are first assigned a value:

Example

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

Running Instance

After the above statements are executed, the variable txt will save the value Hello world!, the variable x will save the value 5, and the variable y will save the value 10.5.

Note:If the value assigned to the variable is text, please enclose the value in quotes.

PHP is a loosely typed language

In the above example, please note that we do not need to inform PHP of the data type of the variable.

PHP automatically converts variables to the correct data type based on their value.

In languages like C and C++ and Java, programmers must declare the name and type of a variable before using it.

PHP Variable Scope

In PHP, variables can be declared at any position in the script.

The scope of a variable refers to the part of the script where the variable can be referenced or used.

PHP has three different variable scopes:

  • local (local)
  • global (global)
  • static (static)

Local and Global scope

FunctionOutsideVariables declared have Global scope and can only be accessed outside the function.

FunctionInternalVariables declared have LOCAL scope and can only be accessed within the function.

The following example tests variables with local and global scopes:

Example

<?php
$x=5; // Global scope
function myTest() {
  $y=10; // Local scope
  echo "<p>Testing variables inside the function:</p>";
  echo "The variable x is: $x";
  echo "<br>";
  echo "The variable y is: $y";
} 
myTest();
echo "<p>Testing variables outside the function:</p>";
echo "The variable x is: $x";
echo "<br>";
echo "The variable y is: $y";
?>

Running Instance

In the above example, there are two variables $x and $y, and one function myTest(). $x is a global variable because it is declared outside the function, while $y is a local variable because it is declared inside the function.

If we output two variable values inside the myTest() function, $y will output the locally declared value, but not the value of $x, because it is created outside the function.

Then, if two variable values are output outside of the myTest() function, the value of $x will be output, but not the value of $y, because it is a local variable and created inside the myTest().

Note:You can create local variables with the same name in different functions because local variables can only be recognized by the function in which they are created.

PHP global Keyword

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

To do this, use the global keyword before the variable (in the function):

Example

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

Running Instance

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

The example above can be rewritten as follows:

Example

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

Running Instance

PHP static Keyword

Generally, all variables are deleted after a function is completed/executed. However, sometimes I need to not delete a local variable. To achieve this, further work is required.

To do this, use it when you first declare the variable static Keywords:

Example

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

Running Instance

Then, every time the function is called, the information stored in this variable is the information contained in the last call to the function.

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