🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

Understand PHP Data Types and Learn to Use Them in Your Scripts

Reading time 5 min
Published Jan 7, 2016
Updated Oct 2, 2019

One of the reasons why PHP is so easy to use is that it does not ask its user to specify the PHP data types of certain PHP variables manually. Instead, it understands it automatically according to the value that has been assigned to it.

PHP variables differ significantly in value. Because of that, PHP can classify variables into certain PHP data types. Nevertheless, it's useful to know and understand what main PHP data types are there, and how they differ from each other.

Basically, eight types can be separated into three bigger groups. There are scalar types - integers, strings, booleans, and floats. More complicated are the compound types - array and objects. Resource and NULL are classified as special types.

PHP Data Types: Main Tips

  • Integers, strings, booleans and floats are the simplest data types.
  • Arrays and objects are considered more complex, as they hold more than a single value. In contrast, NULL holds no value at all.
  • Resource is technically not a data type, because it points to an outside reference.

Strings Explained

PHP strings are sequences of characters. It can consist of letters, numbers or special characters, make up words or sentences. You can learn more about them in a separate tutorial, too.

You must remember to encase the whole string in quotes, though there is no difference if you will use single or double ones:

Example
<?php 
  $txt1 = "Hello world!";
  $txt2 = 'Hello world!';
  echo $txt1;
  echo "<br>"; 
  echo $txt2;
?>

Note: If you want to display quotes inside a string, you need to surround them with a different kind of quotes. For example, if you surround a string with single quotes, you can use double quotes inside, and vice versa.

What Integers Are

Intengers as a data type are used to hold numeric values. It can come in decimal (base 10), hexadecimal (base 16) and octal (base 8) forms. Since PHP 5.4+, binary form (base 2) has also been introduced.

Integers have to follow a couple of rules:

  • They must contain at least a single digit.
  • They have to be whole numbers (no decimal points).
  • They can be positive or negative, but must stay between -2,147,483,648 and 2,147,483,647.

The example below has an integer $x. A function called PHPvar_dump() will return the data type and value of a variable:

Example
<?php 
  $x = -6532;
  var_dump($x);
?>

Description of Floats

If you do use a decimal point or numbers in an exponential form in a variable, it will be classified as a float (floating point number). It can also be positive or negative.

In the example below you can see a float $x. PHP var_dump() function will return the data type and value of a variable:

Example
<?php 
  $x = 201.9865;
  var_dump($x);
?>

Meaning of Booleans

A boolean in an extremely lightweight data type. It can have only one of two values: True or False. You can treat it as a sort of switch, which is useful for conditional statements.

Looking at the PHP class example, notice these values are not put in quotes: by doing that, you would produce a simple string.

Example
<?php
  $submit = true;
  if ($submit !== false)
  {
    echo 'Submitted';
  }
?>

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

Arrays: Lists of Values

Arrays are one of the more complex PHP data types. An array is a single variable that can store multiple values. Those are usually related and of the same type - for example, it can hold a list of your dream countries or all the books on your nightstand.

The example below has an array $x. PHP var_dump() function will return the data type and value of a variable:

Example
<?php 
  $x = array(1, 2, 3);
  var_dump($x);  	
?>

Objects Defined

Objects are different from other PHP data types because they hold not only the data itself but also all the information on how that particular data can be processed. In addition to that, the PHP object has always to be declared.

Firstly, the class of the object has to be declared. To do this, you have to use a class keyword. Classes are structures containing properties and methods.

Let's see an example:

Example
<?php
  class Book {
    function page() {
      $this->pageNumber = '2';
    }
  }
  // create an object
  $book = new Book();

  // show object properties
  echo $book->pageNumber;
?>

What NULL Means

It is a special type of data, which can only have a single value: NULL. NULL variables have no real value assigned to them.

Variable values can be cleared by setting them to NULL. Variables that are created without an assigned value are assigned the value of NULL by default.

Example
<?php
  $txt = "Hello world!";
  $txt = null;
  var_dump($txt);
?>

Resources Identified

Resources technically shouldn't even be classified as one of PHP data types: it's more like storage of a reference to various resources outside of PHP. Examples of such external resources might be databases or opened files.

PHP Data Types: Summary

  • There are eight PHP data types in total: integers, strings, booleans, floats, arrays, objects, NULL and resource.
  • Arrays and objects hold more than a single value. NULL holds no value at all.
  • Resource is used to direct the used to an outside reference.