Different Types of PHP Variables

Published on June 6th, 2014 by | Category: Programming

Please make sure to read about the PHP Variables syntax here before you read this article.

There are different types of PHP variables. The most important ones are: Numbers, Strings, Arrays, and Objects


There are two types of numbers in PHP programming. “Integers” are the whole and round numbers. They have no decimals and fractions. They can be positive or negative, but they can not have decimals or fractions. For example, 1 and -1 are integer. But 1.0 or 0.1 or 1/2 is not integer.

The other type of numbers is know as floating-point, double-precision or doubles. Floating-point numbers can have decimals. For example, 1.23 is a floating-point number. We can not have fractions in PHP. Therefore , we have the convert the fraction value into decimal and use it in PHP. For example, instead of 1¼ we must have 1.25.


String is any character inside (“) or (‘). It can be letters, number, or any character. For example:

“Hello World!”
‘Hello World!’

Please note that there are occasions that you will be faced with problems when you have (“) inside the strong. For example if you use the string as below you will receive an error:

“Hey Peter, “What are you doing?””

We have talked about this issue, its reason and its solution in our Basic PHP Syntax article. The problem can be fixed like this:

“Hey Peter, \”What are you doing?\””


“Hey Peter, ‘What are you doing?'”


‘Hey Peter, “What are you doing?”‘


String has just one value. However, an array is a variable that has several values. Yes, it is possible to give several values to one variable. Each value in each array has a key. If the keys are numbers, the array is called indexed array. When the keys are strings, then the array is called associative array.

For example let’s have an array variable with $_names as its name. We can define this variable like this:

$_names = array (‘Peter’, ‘Mike’, ‘Eric’);

When we define the array the way that it was defined above, then the system starts defining the key for each value from 0. Therefore, each of the above 3 values will have the key as below:

0 for Peter
1 for Mike
2 for Eric

However, we can define the keys too:

$_names = array (1 => ‘Peter’, 2 => ‘Mike’, 3 => ‘Eric’);

In can be coded in multiple lines too:

$_names = array (
1 => ‘Peter’,
2 => ‘Mike’,
3 => ‘Eric’

If coded as above, then each value will have its defined key:

1 for Peter
2 for Mike
3 for Eric

What about the associative arrays?

Let’s have an associative array here:

$_Foods = array (
‘Monday’ => ‘Pizza’,
‘Tuesday’ => ‘Beaf Burger’,
‘Wednesday’ => ‘Cheese Burger’,

When defined like above, then each value will have a different string key:

Monday for Pizza
Tuesday for Beaf Burger
Wednesday for Cheese Burger

So, above we have a variable that its name is $_Foods. This variable is an array because it has more than one value.

Now, as an exercise let’s have the above variable in a PHP script, upload it to the server or use the XAMPP to see how it works. Please have the below code in a .php file and run it:

Different Types of PHP Variables

Hey Peter, \"What are you doing?\"

"; // this is an example of an "array" type variable: $_array = array ( 'Monday' => 'Pizza', 'Tuesday' => 'Beaf Burger', 'Wednesday' => 'Cheese Burger', ); print_r($_number); print_r($_string); print_r($_array); ?>

If you have done everything right, you see the below output in your browser screen:

The first line shows our number variable. The second line shows our string variable. And the rest of the page shows the array which has 3 values that each of them has a different key.

So, to assign a value to a variable, you should use the “equal sign”. Like this:

$_number = 12345;

$_string = “Hey Peter, \”What are you doing?\””;

Above, we have two variable: $_number and $_string

They could be defined with any name. Like this:

$_first = 12345;

$_second = “Hey Peter, \”What are you doing?\””;

This is how you define the “number” and “string” variables. However, as you see above, there are some different ways to define the “array” variables.

This was just an introduction on the different types of variables. We will talk about each of them more. Also, we will not talk about the “Objects” here in this article, because it is a complicated discussion that does not fit this article.

Leave a Reply

Your email address will not be published. Required fields are marked *