Datatypes in PHP

As we already know about the Variable Scopes, let’s check out the supported data types in PHP. There are a total of 8 datatypes in PHP, which helps us identify the type of data a particular variable holds.

There happens an implicit conversion of datatypes in PHP, which means that we don’t specify the datatype while declaring a PHP variable. Based on the value that gets assigned to a variable, PHP engine implicitly determines the datatype and assigns it to that variable. To understand this better, let’s check out the PHP code below:

Note that:

  1. Variable a has just been declared without initialization. Though it’s not recommended, variable a in this case holds a NULL value. We can also say that, variable a is still unaware of what type of data it is going to hold.
  2. Variable b has been declared and initialized with a value of 10. So, as expected, var_dump-ing it, prints 10 with an implicit cast of variable b to int.
  3. Variable a now gets initialized with a value of 10.50. So, as expected, var_dump-ing it, prints 10.5 with an implicit cast of variable a from type NULL to int.
  4. Variable b now gets assigned a value of 11.50. So, as expected, var_dump-ing it, prints 11.5 with an implicit cast of variable b from type int to float.

print_r and var_dump are excellent debugging tools in PHP. They are very much similar to the print and echo functions. Just that print_r and var_dump provide more detailed information about a PHP variable, they are preferably used only for debugging purpose.

We can also say that datatypes help in constructing the variables.

The datatypes supported by PHP are:


  1. String – anything within double quotes can be called as a String. Eg: $a = “Yash”;
  2. Integer – any number. Eg: $b = 5;
  3. Float – any number with a decimal point. Eg: $c = 1.5;
  4. Boolean – true/false Eg: $d = true;
  5. Array – collection of similar datatypes. Eg: $e = array(1,2,3,4,5);
  6. Object – will be learn about this in Object Oriented PHP
  7. NULL– a variable without having any value Eg: $f = null;
  8. Resource – not actually a datatype. It stores the reference to functions and resources external to PHP, which we will be exploring more when we connect our PHP application with MySQL.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.