PHP Data Types and Variables Explanation

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
PHP Data Types

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 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 Data Types

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 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.
Valid variable declaration in PHP programming

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.

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, Tailwindcss, Bootstrap, JavaScript, jQuery, Vue.js, WordPress and Laravel framework. Follow Me: Facebook, Twitter and Linkedin.

Related Posts

Laravel First Time Installation on Windows 10/11

Laravel is one of the best full stack frameworks used to develop modern web application simply and securely. Day by day PHP developers are absorbing Laravel. So,…

PHP Delete Array Element with Built-in Function

Array is an important and mostly used data type in PHP programming. PHP programmer can manually add element to an array or delete element from an array…

PHP Add Element to Array with Built-in Functions

Array plays an important role in PHP programming. It is one of the most used data structures in PHP programming. So, it is better to have a…

PHP Array Explanation

Array is one of the most important and basic terms in PHP Language. Almost all the PHP program uses array data structure. So, those who want to…

Function Call by Value and Call by Reference in PHP

More than thousands of built-in functions proves the power of PHP Language. Besides built-in function PHP allows to create user defined function. How to create user defined…

PHP Variable Usage inside of Function

Besides more than thousands of built-in functions PHP provides facility to define custom/user defined function. How to define and call a user defined function was discussed in…

Leave a Reply

Your email address will not be published.

*

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