PHP Variables

PHP variables start with the dollar sign ( $ ), then the name of your variable and then the value. When you assign a variable it can only start with an underscore or a letter (upper case or lower). Variable names cannot start with a number or any other symbol.

In other programming languages, when you create a variable you also must specify the data type, e.g. Boolean, integer or string. In PHP you do not do that, because PHP is a "loosely-typed" language. This means that PHP will automatically detect the data type of the variable.


$name = 'content';

Legal names:

  1. $name
  2. $Name
  3. $_name
  4. $_Name

Illegal Names:

  1. $5Name
  2. $@name

Data Types

These are the common data types that PHP supports:

Data Type Description
Boolean True or false
Integer Whole numbers
Strings Set of characters
Floating point numbers Numbers with decimals
Arrays Collection of variables

When you create a variable and assign it a value, PHP will automatically detect the type of data (e.g. string, integer). Other languages such as C# are "strongly-typed" languages and require you to specify the data type. In PHP the variables can contain any data, and you can create a variable and store a string in it but later change it to an integer. This is good as it allows flexibility within your PHP scripts, however it can also cause bugs.

Let's write some code: Create a new PHP file and name it variables.php.



$name = 'Asim';
$Age = 18;
$Height = 5.8;


In this example we have created three variables: $name (string), $Age (integer), and $Height (floating point). Next we will print these values out using the echo command.


echo $name. ' is '.$age. ' years old, his height is '.$height.' ft.


The dot (period) is the concatenation operator, and echo is a command used to print out text to the browser. You will learn more about these in the next tutorial.


Asim is 18 years old, his height is 5.8 ft.


PHP provides many built-in functions; one particular function for variables is the gettype function, which gets the data type of a variable.


echo gettype($name);



There are many more functions you can use to determine the type of variable. The following are a few:

Function Description Output
is_string (value) Checks if a variable is of string data type Boolean
is_null (value) Checks if a variable is null Boolean
is_int (value) Checks if a variable is integer Boolean
is_bool (value) Checks if a variable is boolean Boolean
is_numeric(value) Checks if a variable is a number Boolean

These functions take one parameter: the variable name.

Example is_int()

if (is_int($age))
echo 'True';

echo 'False';





This tutorial covered PHP variables as well as the functions available.

  • Variables must start with a $ sign
  • The first variable character must be a letter or underscore
  • Variables are case sensitive
  • Variables are loosely-typed (you can change the data type)
  • You do not specify the data type, as PHP will automatically recognise it
  • You can find a variable data type by using the gettype( variable name) function