PHP Data Type and Variable Explanation

Data type and variable are frequently used topics in PHP programming. So, PHP programmer should have basic and clear concept on PHP data types and variables. Various types of data are used in PHP programming and variables usually hold these data in memory for a certain time. In my previous article I discussed PHP basic program with example and in this article I will discuss basic concept on PHP data types and variables.

PHP Data Type and Variable
PHP Data Type and Variable

PHP Data Types

Like other programming languages, various types of data are used in PHP programming language. These data can be categorized into 8 types. The following table is a summarization of the various data types used in PHP Programming Language.

No.Data TypesDescription
1IntegerReal number without decimals. Such as 1, 5, 100, 1000 and so on
2FloatReal number with decimals. Such as 35.5, 100.10 and so on.
3StringSum of several characters that means a word, a sentence or several sentences
4BooleanEither true or false value. True represents 1 and false represents 0.
5NULLThis is a special data type in PHP which has no data value.
6ArrayVarious types of data are kept within this type with index value
7ResourceThese types of data come from database
8ObjectThis 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 Variable

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. We will discuss these variables at the respective topics.  

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.


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 PHP 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. Generally Capital letter is used to declare Constant.

Like other 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.

Example of Valid Variable Declaration in PHP

Now we will see some example of valid variable declarations in PHP coding. The following examples are valid PHP variable declarations those will make your concept clear.

Example No.Variable DeclarationDescription
1$num=20Declaring a variable named num and assigning an integer value.
2$average=50.5Declaring a variable named average and assigning a float value.
3$firstName=”Mahbub”Declaring a variable named firstName and assigning a string value.
4$status=trueDeclaring a variable named status and assigning a boolean value.
5$sum=NULLDeclaring 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.

Unlike some strict programming languages, PHP is intelligent enough to understand assigned variable data type. So, we don’t need to declare variable data type explicitly in PHP coding.

Example of Invalid Variable Declaration in PHP

We will now see some invalid variable declarations in PHP coding. Invalid variable declaration causes error in program compilation. So, we should be serious when declaring any variable in any 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 Type and Variable Usage

We will now do a small PHP program where PHP data type and variable have been used. The following program will show proper use of PHP data type and variable.

  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 three numbers and then we have calculated the average of those three numbers.  At last, 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 type and variable have been explained in this article. I hope, you have now clear concept on PHP data type and variable. 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.

Related post:



I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, JavaScript, AJAX, jQuery, WordPress and CodeIgniter framework. Follow Me: Facebook, Twitter and Linkedin.

Your name can also be listed here. Have an IT topic? Submit it here to become a iTechSheet author.

One thought on “PHP Data Type and Variable Explanation

Leave a Reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.