PHP Variables

Summary: in this tutorial, you will learn how to use PHP variables to store and manipulate data.

php variableDefining 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:

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:

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:

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:

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:

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:

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:

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 NameMeaning
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:

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.

To unset a variable, you use the  unset() function. The following example illustrates how to use the  unset() function:

Checking NULL and empty

To check if a variable is NULL, you use the  is_null() function

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 existThe following example shows you how to use the empty() function:

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:

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:

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 from  GET requests.
  • $_POST contains information from POST 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:

How it works.

  • First, we defined the set_counter() function with a static variable named $counter. Inside the set_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 the set_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:

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.

  • Was this tutorial helpful ?
  • YesNo