Data Types in PHP | PHP Tutorial Part -2

Data Types in PHP | PHP Tutorial Part -2

17 June 2023 1 By Anshul Pal

In a previous article we already discussed the “Introduction of PHP,  Installation of PHP, Syntax of PHP, and variables in PHP”. Now in this tutorial we learn data types in PHP. There are eight different types of Data Types in PHP. Similar to Python and C, only one additional type of data type is here, which is “Resource”.

Data Types in PHP

In the Hypertext Preprocessor programming language there are a total of eight different types of data types, as you can see below.

  1. Integer
  2. Float
  3. String
  4. Boolean
  5. Array
  6. Object
  7. Null
  8. Resource

These are all categorized in three main ways :-

  • Scalar Types (predefined) It holds only single value.
  • Compound Types (user-defined) It can hold multiple values.
  • Special Types Special type data type.

PHP Data Types


In programming languages, we know that an integer data type is known as counting numbers or the whole numbers(positive and negative) without a decimal point. Integers data types are non-decimal numbers between -2,147,483,648 and 2,147,483,647. They are the simplest data types. The size of integers depends on the platform where PHP runs.

Rules for integer:

  • An integer does not contain any decimal points.
  • It is mandatory that an integer have at least one digit.
  • An integer is either positive or negative.
  • An Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16).


$x = 598;
$y = 456;
echo $x+$y;

In the above example, you can see that we are taking two different types of integer data types whose values are stored in variables $x and $y and after that we add both of them and print the output with the help of the echo keyword.


Here in this image you have seen the output of the previous program.

Congratulations!  You have made your first PHP program.


A string is a combination of characters. It holds letters, alphabets, numbers, and even special characters. A string is a non-numeric data type. String values are surrounded by single quotes () or double quotes ().

$b = 'Welcome to PHP string';
echo $b;

Note –  You can also use doubles quotes to represent a string.

Welcome to PHP string

PHP String Methods

  • strrev() – You can use this function to reverse a string.
  • str_replace() – It replaces some characters with some other characters in a string.
  • strlen() – It returns the length of the string.
  • str_word_count() – This function counts the number of words in a string.
  • strtolower() – It converts the whole string into lower case.
  • strtoupper() – It converts the whole string into the upper case.
  • strcmp() – This method helps to compare two strings.
  • trim() – This function removes whitespace and other predefined characters from both sides of a string.

To learn more methods of PHP strings click here.


Float stands for Floating point number. Those types of numbers, which are in exponential form or numbers with a decimal point, include negative or positive signs. These numbers are also known as double or real numbers. Floating point numbers are larger than integers.


$a = 15.895;
echo $a;


Boolean data types are used in the case of conditional testing. A Boolean data type holds only two values : true or false. If the given condition is correct, then it returns true and if your condition is wrong, then it returns false. If a string is empty, then it is considered false in the Boolean data type.

Example –

$a = 20;
if ($a == 20) {
  echo "This is True condition";
} else {
  echo "This is false condition";
This is True condition


An array is a data type which can store multiple values only in one single variable. An array is a type of compound data  type that can hold many values under a single name, and you can easily access these values by its index numbers. In PHP there are three types of arrays :

  1. Associative arrays -.An array with strings as index.
  2. Indexed arrays – An array with a numeric index.
  3. Multidimensional arrays – Values can be accessed using multiple index..

Example –

$name = array("Kishore", "Arjun", "Tanmay");
echo "My bestfriends are " . $name[0] . ", " . $name[1] . " and " . $name[2] . ".";
My bestfriends are Kishore, Arjun and Tanmay.


Objects are defined as instances of user-defined classes. It can hold both values and functions. Classes and objects are the two main aspects  of object-oriented programming. A class is a template for objects, and an object is an instance of a class.

     class car {
          function model() {
               $model_name = "Fortuner 4X4";
               echo "car Model: " .$model_name;
     $obj = new car();
     $obj -> model();
car Model: Fortuner 4X4

Null Data Types

It is a special data type which consists of only  one value. Those types of variables which have no assigned value are known as Null data types. Null values are used to represent empty variables in PHP. If you create a variable without a value, then it is automatically assigned a Null value.


$a =NULL;

Resources Data Types

Resources are the data types which are used to store some function calls or references to external PHP resources. It is not an actual data type in PHP.

Example – A file handle or a data base connection.

Type Casting in PHP

The Hypertext Preprocessor allows type casting or type conversion. It helps developers easily convert one type of value to another type. In PHP there is no need to define the data type of a variable, which is similar to the python programming language. PHP variables automatically decide the data type of variable on the basis of the given content.

We can cast the following data type variable in PHP.

  • int – cast to integer
  • float, real, double  – cast to float
  • string – cast to string
  • unset – cast to NULL (PHP 5)
  • bool – cast to boolean
  • object – cast to object
  • array – cast to array

Example –

$a = 15.5; // float to integer  
echo "Conversion from float ",$a," to ";  
$a = (int)$a;  
echo "int ", $a;  

In the above example, we only convert a float value into an integer value. You can convert the remaining data types similarly. First, we take a float value and then, by using type casting, we convert variable type to float and then, with the help of the echo keyword, we simply print the output as you can see below.


Conversion from float 15.5 to int 15

You may also like this: –

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.