PHP | Data Types

PHP data types are used to express different data types that are associated with the variables or function return's type.

PHP Variable Types

PHP variables are used to hold different data types such as Integer, Floating point number or Float, String, Boolean, Array, Object, resource, and NULL. The data types in PHP are classified into three categories.

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types

PHP Scalar Data Types

There is 4 scalar data type in PHP and it contains a single value.

  • boolean: It is used for true or false values.
  • integer: It is used for whole numbers.
  • float: It is used for real numbers.
  • string: It is used for strings of characters.

PHP Compound Data Types

It can hold multiple data and it is categorized into two parts.

  • array: It is used to store multiple data items.
  • object: It is used for storing instances of classes.

PHP Special Data Types

There are 2 special data types in PHP.

  • resource: Database function return variable will be of type resource.
  • NULL: It represents a variable with no value.

PHP Integer Data Type

PHP integers are basically whole numbers without decimal points such as 1,2,3,-1,-2,0, etc.

Rules For Integer Data Type:

  • An integer does not contain a decimal point.
  • It can be a positive or negative number..
  • Integers can not contain empty spaces.
  • It has at least one digit(0-9).
  • Integer can be expressed as a decimal (base 10), octal (base 8), hexadecimal (base 16), or binary(base 2).
  • The range of an integer varry between 2,147,483,648 and 2,147,483,647 i.e., -2^31 to 2^31.

Source Code

$a = 120;
// decimal number
$a = -123; 
// a negative number
$a = 0x40; 
// hexadecimal number
$a = 0300; 
// octal number
$a = 0b10100000;	
// binary number
Try it now

PHP Float Point Numbers

Floating point numbers ("floats", "doubles" or "real numbers") are numbers with a decimal point or a number in exponential form.

Source Code

    $x = 24.365;
    echo "<br />";
    $x = 1.5e3;
Try it now

PHP String Data Type

A string is a finite sequence of characters that is enclosed with single or double quotes. The string characters might be letters, numbers, special characters, symbols, etc.

String Represented Method

There are four ways to represent a string. These are:

  • Using Single Quote
  • Using Double Quote
  • Using Heredoc Syntax
  • Using Nowdoc Syntax

Source Code

$str = 'This is single quoted text';
$str = "This is double quoted text";

$str = <<<EOD
This is a heredoc syntax that is used
to represent string text

$str = <<<'EOD'
This is a nowdoc syntax that 
is used to represent string text 

Try it now

PHP Array

PHP array is a variable that holds multiple values in a single variable. There are two ways to represent an array:

  • It can be specified by the array() function.
  • It can also be represented with [ and ].

Source Code

$fruits = array('Apple', 'Banana', 'Orange', 'Mango');
Try it now

PHP Objects Data Types

PHP object is basically is an instance of a user-defined class that has the ability to access instance variables and functions. Every object is completely independent of the other because they have their own properties and methods.

Source Code

// Class definition
class Resources{
// properties
public $learningResource = "sudhakarinfotech";
 // methods
 function displayResources(){
   return $this->$learningResource;
// Create object from class
$obj = new Resources;
Try it now


PHP NULL value is a special variable that has no value. It identifies whether a variable is empty or not. We can use the is_null() function to check any value if it is NULL.

Understand The Basic Differences:

When a user has created a variable like $var then a null value is automatically assigned to it. Therefore, keep in mind that $var1=null and $var2="" is not same. Here $var1 represents null value while $var2 represent empty value.

Source Code

$a = NULL;
echo "<br>";
$b = "Hello World!";
$b = NULL;
Try it now

PHP Resources

It is a special variable that holds the reference of other external resources. Basically, it belongs to the database function.

Source Code

	$con = mysqli_connect("localhost","root","");
Try it now

Data Type Related Functions in PHP

gettype() – data type getter returns given variables data types.

var_dump() – This function is used to get the data type of an expression.

is_() – There are several variable functions like is_array(), is_null() and etc., to check a variable with respect to specific data type.

settype() – This is the data type setter, used in PHP type conversion.

Web Tutorials

PHP | Data Types
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4