Data type and variable are frequently used terms in PHP programming. Without knowing data types and variable, PHP program cannot be imagine. So, PHP programmer should have a clear concept on PHP data types and variables. PHP uses various data types and variables are used to keep these data in memory for a certain time. A PHP program actually plays with various data types keeping them in variables.
In my previous article, I discussed how to launch your First PHP Program in a local development environment. In this article I am going to discuss the basic concept of PHP data types and variables.

PHP Data Types
Like other programming languages, PHP uses variaous data types. PHP data types can be categorised into the following three types.
- Scalar
- Composite/Compund
- Special
Scalar Data Types
Scalar is a single unit of data. There are four scalar data types in PHP language. These are:
- Integer
- Float
- String and
- Boolean
Composite Data Types
Composite is a collection of data. There are two composite data types in PHP language. These are:
- Array and
- Object
Special Data Types
There are two special data types in PHP programming language. These are:
- NULL and
- Resource
The following table is a summarization of various data types used in PHP Programming Language.
No. | Data Types | Description |
1 | Integer | Real number without decimals. Such as 1, 5, 100, 1000 and so on |
2 | Float | Real number with decimals. Such as 35.5, 100.10 and so on. |
3 | String | Sum of several characters that means a word, a sentence or several sentences |
4 | Boolean | Either true or false value. True represents 1 and false represents 0. |
5 | NULL | This is a special data type in PHP which has no data value. |
6 | Array | Various types of data are kept within this type with index value |
7 | Resource | These types of data come from database |
8 | Object | This type of data is used in object oriented programming. Since PHP can be used as object oriented programming language, this is one kind of type in PHP. |
PHP Variables
Variable can be considered as a container of data. User given data or program assigned data are stored in variables and then used in different place in program. According to the usage or position, variable can be different types such as local variable, global variable, static variable, session variable, environment variable and so on.
Before using PHP variables we need to declare it in our program. In the following section we will learn how to declare variables in PHP coding.
Variable Declaring in PHP Language
Any name that starts with a dollar ($) sign is considered as a variable in PHP programming language. So, if we want to declare a variable, we have to start the name with a Dollar ($) sign. The following line is an example of declaring variable in PHP programming.
$first_name
Here, the first_name token has started with a Dollar ($) sign. So, it is a variable where we can keep any name of any person. Variable name can be any string but there are some rules and restrictions those we have to remember and follow otherwise PHP compiler will generate an error message against variable declaration.
PHP Variable Declaration Rules
Like other popular computer languages, PHP language has some rules and regulation to declare variable in coding. The following lists are a summarization of these rules and regulations.
- Variable must start with a character or underscore ( _ ) but not with number.
- There will be no space between variable names.
- Variable should not start with operator like (+, -, *, / or . ). Also mathematical operator cannot be used within variable name.
- It is best practice to write variable with small letter. Capital letter is generally used to declare Constant.
Like other programming languages, empty variable declaration is not allowed in PHP language. So, we should always keep in mind to assign a data value when we declare a variable in PHP coding otherwise PHP compiler will generate an error message. If we do not require assigning a value at the time of variable declaration, we can trickily assign NULL data which will make our code error free.
Unlike some strict programming languages, PHP allows variable declaration at any place of program coding but it is always better and good practice to define variables at program starting position.
PHP is a loosely typed programming language. So, there is no need to tell the data type of the variable. PHP automatically associates a data type to the variable depending of its assigned value. As data type is not set in a strict sense, we should be careful enough to handle variable value specially when we pass data to a function.
Type declarations were added in PHP 7. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a “Fatal Error” on a type mismatch. We will learn strict and non-strict requirements and data type declaration when we learn PHP function elaborately.
Example of Valid Variable Declaration in PHP
We will now see some example of valid variable declarations in PHP coding. The following table is a list of some valid variable declarations of different data types.
Example No. | Variable Declaration | Description |
1 | $num=20 | Declaring a variable named num and assigning an integer value. |
2 | $average=50.5 | Declaring a variable named average and assigning a float value. |
3 | $firstName=”Mahbub” | Declaring a variable named firstName and assigning a string value. |
4 | $status=true | Declaring a variable named status and assigning a boolean value. |
5 | $sum=NULL | Declaring a variable named sum and assigning a NULL value. That means its value will be assigned later. |
6 | $countries=array() | Declaring an empty array variable named countries. |
In PHP, there is no need to define data type of a variable explicity. PHP will automatically associate data type to a variable depending on its assigned value.
Example of Invalid Variable Declaration in PHP
We will now see some example of invalid variable declarations in PHP coding. Invalid variable declaration causes error in program compilation. So, we should be careful enough when declaring any variable in PHP program. The following lines are some invalid variable declaration examples in PHP language.
Example: $1num=10
This is an invalid variable declaration because the variable name starts with a numeric character.
Example: $first name =”Mahbub”
This is also an invalid variable declaration because there is a space between two variable name tokens.
Example: $last+name=”Hasan”
This is also an invalid variable declaration because there is an operator between two variable name tokens which is not allowed in PHP coding.
A Tiny PHP Program of Data Types and Variables Usage
We will now write a small PHP program where PHP data types and variables have been used. The following program will show proper use of PHP data types and variables.
<?php
$firstNumber=55;
$secondNumber=70;
$thirdNumber=89;
$numberCount=3;
$sum=$firstNumber+$secondNumber+$thirdNumber;
$average=$sum/$numberCount;
echo "The sum of ".$numberCount." numbers is ".$sum."<br/>";
echo "The average of ".$numberCount." numbers is ".$average;
?>
Program Output
The sum of 3 numbers is 214
The average of 3 numbers is 71.333333333333
In the above program, we have just assigned three integer data to the three variables. We have calculated the sum of those three numbers and then we have calculated the average of those three numbers. Finally, we have printed out those sum and average with echo function.
Notice that we have used dot (.) operators to concatenate string. Like other programming languages, PHP uses various operators to make expression. PHP operators and expressions will be discussed in our next topic.
PHP data types and variables have been explained in this article. I hope, you have now clear concept on PHP data types and variables. However, if you face any confusion to understand PHP data type and variable, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you.