Summary: in this tutorial, you will learn how to use PHP variables to store and manipulate data.
Table of Contents
Defining PHP variables
A variable stores a value of any type e.g., a string, a number, an array, an object, or a resource. A variable has a name and value. You use the following syntax to define a variable:
1 | $variableName = value; |
You must follow the naming rules of variables when you define variables in PHP:
- The name of a variable must start with the dollar sign (
$
). - The first character after the dollar sign (
$
) must be a letter (a-z
) or an underscore (_
) - The remaining characters can be underscores, letters or numbers.
PHP variables are case-sensitive. It means that $abc
and $AbC
variables are completely different variables.
The following example defines two integer variables:
1 2 3 | <?php $x = 10; $y = 20; |
Variable data types
PHP is a loosely typed programming language. It means that when you define a variable, you don’t specify its data type. PHP will pick an appropriate data type for the variable automatically based on the value you assign to the variable. In addition, the type of a variable changes when you assign different types of values. See the following example:
1 2 3 | <?php $x = 20; $x = 'This is a string'; |
First, we defined $x
variable as an integer because we assigned its value 20. Second, we assigned a literal string to $x
therefore the data type of $x is now string
instead of integer
.
PHP provides the following data types:
- Scalar data types: integer, float, string and Boolean.
- Compound data types: array and object.
- Special data types: null and resource
For more information on PHP data types, check it out the PHP data types tutorial.
Type casting
PHP handles the type conversion automatically based on the context in which you use the variables. In the following example, PHP converts a string into a number and uses the number for calculation:
1 2 3 4 5 6 7 | <?php $x = 20; $y = '10'; // a string $z = $x + $y; // $y is converted to an integer echo $z; // 30 |
However, for the security reason, you should cast the variable’s data type to the desired data type before using it. To force PHP to use a variable as a specific type, you put name of the desired data type in parentheses before the variable. This is known as type casting. Take a look at the following example:
Take a look at the following example:
1 2 3 4 5 6 7 | <?php $x = 20; $y = '10'; // a string $z = $x + (int)$y; // $y cast to an integer echo $z; // 30 |
Notice that the data type of the $y
variable does not change. To change the data type of a variable, you use the settype()
function, which we will discuss in a few seconds.
Finding data types of variables
PHP provides a built-in function gettype()
that returns the data type of a variable. The gettype()
function accepts a variable as a parameter and returns its data type. The following example demonstrates how to use the
The following example demonstrates how to use the gettype()
function:
1 2 3 4 5 6 7 8 9 10 | <?php $int = 10; // integer $str = 'this is a string'; // string $bool = true; // boolean $d = 3.14; // float echo gettype($int), '<br>'; echo gettype($str) , '<br>'; echo gettype($bool), '<br>'; echo gettype($d), '<br>'; |
Changing data type of variables
To change the data type of a variable, you use the settype()
function. To use the settype()
function, you pass a variable that you want to change data type and desired data type. The
The settype()
function will preserve the value of the variable as much as possible. See the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $f = 20.05; echo $f . '<br>'; // float number settype($f, 'integer'); echo $f . '<br>'; // 20 integer number settype($f, 'string'); echo $f . '<br>'; // '20' string settype($f, 'float'); echo $f . '<br>'; // 20 float number |
Testing data types of variables
PHP provides a list of useful functions that allow you to test data types of variables. The following table illustrates those functions:
Function Name | Meaning |
---|---|
is_int($var); | Return true if $var is an integer, otherwise return false. |
is_string($var); | Return true if $var is a string, otherwise return false. |
is_bool($var); | Return true if $var is a Boolean, otherwise return false. |
is_float($var); | Return true if $var is a float, otherwise return false. |
is_long($var); | Return true if $var is a long type, otherwise return false. |
is_numeric($var); | Return true if $var is a number, otherwise return false. |
is_double($var); | Return true if $var is a double, otherwise return false. |
is_object($var) | Return true if $var is an object, otherwise return false. |
is_array($var); | Return true if $var is an array, otherwise return false. |
Variable variables
PHP allows you to reference the value of a variable whose name is stored in another variable. See the following example:
1 2 3 4 5 6 | <?php $foo = 'bar'; $$foo = 'this is $bar'; echo $bar; // this is $bar |
First, we defined $foo
variable and assigned it a literal string, bar
. Next, we used $foo
and assigned it to another literal string. $foo
means bar
and $foo
means $bar
variable; therefore this assignment created a new variable whose name is the value of the $foo
variable which is bar
. Third, we displayed the value of the $bar
variable.
Set and unset variables
When we assign a variable a value, either literal value or another variable’s value, we say that the variable is set. You can check if a variable is set using the isset()
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php $x; // $x is not set if (isset($x)) { echo '$x is set. <br/>'; } else { echo '$x is not set. <br/>'; } $y = 10; // $x is set if (isset($y)) { echo '$y is set. <br/>'; } else { echo '$y is not set. <br/>'; } |
To unset a variable, you use the unset()
function. The following example illustrates how to use the unset()
function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php $x = 10; // $x is not set if (isset($x)) { echo '$x is set. <br>'; } else { echo '$x is not set. <br>'; } unset($x); // $x is not available anymore if (isset($x)) { echo '$x is set. <br>'; } else { echo '$x is not set. <br>'; } |
Checking NULL and empty
To check if a variable is NULL
, you use the is_null()
function.
1 2 3 4 5 6 7 8 9 | <?php $x = null; echo is_null($x) ? '$x is null' : '$x is not null'; echo '<br />'; $x = 20; echo is_null($x) ? '$x is null' : '$x is not null'; |
To check if a variable is empty, you the empty()
function. A variable is considered to be empty if its value equals to false
or it does not exist. The following example shows you how to use the empty()
function:
1 2 3 4 5 6 7 | <?php $x = 0; echo is_empty($x) ? '$x is empty' : '$x is not empty'; $s = ''; echo is_empty($s) ? '$s is empty' : '$x is not empty'; |
PHP variable scopes
The scope of a variable determines which parts of the script can access it. The location where the variable is defined determines the scope of a variable.
There are four types of variable scope in PHP: local, global, static and function parameters.
Local variables
A variable defined in a function is local to that function. It means a local variable only can be accessible by the code inside the function where it is defined. See the following example:
1 2 3 4 5 6 7 8 9 | <?php function foo() { $bar = 10; // $bar += 20; return $bar; } |
The $bar
variable is a local variable inside the foo()
function. It cannot be accessible outside of the foo()
function.
Global variables
A variable declared outside a function has a global scope or is called a global variable. A global variable is accessible from any part of the program. However, by default it is not available inside the functions. To refer to a global variable inside a function, you use the global
keyword when declaring a variable like the following example:
1 2 3 4 5 6 7 8 9 10 11 | <?php $x = 20; function test() { global $x; echo $x; //20 } test(); |
Super global variables
PHP provides a list of special global variables which are known as superglobal variables. The superglobal variables provide information about the PHP script’s environment. The super global variables are automatically available in any PHP script file.
The following is the list of PHP super global variables:
$GLOBALS
is an array that contains global variables. The variable names are used to select which part of the array to access.$_SERVER
contains information about the web server environment.$_GET
contains information fromGET
requests.$_POST
contains information fromPOST
requests.$_COOKIE
contains information from HTTP cookies.$_FILES
contains information from POST file uploads.$_ENV
contains information about the script’s environment.$_REQUEST
contains user inputs. The$_GET
or$_POST
should be used instead of$_REQUEST
as they are more specific.$_SESSION
contains information from any variables registered in a session.
Notice that as PHP 4.0.1, super global variables are defined as arrays.
Static variables
A static variable retains its value between function calls and it is only accessible inside that function. You use the static
keyword to define a static variable. For example:
1 2 3 4 5 6 7 8 9 10 | <?php function set_counter() { static $counter = 0; $counter++; echo $counter . '<br/>'; } set_counter(); // 1 set_counter(); // 2 set_counter(); // 3 |
How it works.
- First, we defined the
set_counter()
function with a static variable named$counter
. Inside theset_counter()
function, we increased the counter’s value by 1 and displayed it. - Second, we called the
set_counter()
function three times. Each time we call theset_counter()
function, the$counter
value was increased by 1; You can notice that its value retained in the subsequent function calls.
Function parameters
Function parameters are local and they can be only visible inside the function. See the following example:
1 2 3 4 5 | <?php function to_html($str) { // } |
The $str
is a parameter of the to_html()
function. It is only available inside the to_html()
function. You will learn more about function in the PHP function tutorial.
In this tutorial, we have covered a lot of stuff about PHP variables. First, we started by showing you how to define variables and how to deal with variable data types. Then, we introduced some useful functions that deal with variables. Finally, we discussed four different types of variable scopes in PHP.