This article may contain affiliate links. If you buy some products using those links, I may receive monetary benefits. See affiliate disclosure here

In this post, let’s take a quick look at the different data types PHP offers:

1. Boolean

A boolean variable can be either true or false. For example:

// 1. Booleans - true or false value

$check = true;

var_dump($check);
echo gettype($check);
echo '<br>';

2. Integers

The integer data type can contain numbers, positive or negative, and zero.

// 2. Integers 

/*
- negative or positive integers, and zero
- can be decimal, binary, octal, or hexadecimal
*/

$score1 = 80;
var_dump($score1);

$score2 = -25;
var_dump($score2);

var_dump($score1 + $score2);

3. Floats

The float data type can contain numbers with decimal places.

// 3. Floats - decimal numbers

$average = ($score1+$score2)/2;
var_dump($average);
var_dump((int)$average);

4. Strings

A string can contain letters, alphabets, numbers, special characters, etc. Each character will be 1 byte in size.

Strings can be created using double quotes, single quotes, heredoc, or nowdoc, as given below.

// 4. Strings

/*
- is a sequence of characters, each 1 byte in size
- double quotes, single quotes, heredoc, or nowdoc
*/

$person = "Abhinav";
$channel = 'Coding Reflections';

$message = "$person is recording a video for $channel";
$message2 = '$person is recording a video for $channel';

echo $message;
echo '<br>';
echo $message2;
echo '<br>';

$codeBlock = <<< HERE
<pre>
    if(true) {
        echo "hello world!";
        echo "second line";
        if(true) {
            echo "third line";
        }
     }
</pre>
HERE;
echo $codeBlock;

$codeBlock = <<< 'NOW'
<pre>
    $a = true;
    if($a) {
        echo "hello world!";
        echo "second line";
        if(true) {
            echo "third line";
        }
     }
</pre>
NOW;
echo $codeBlock;

5. Arrays

Array is compound data type. You can think of an array as a series of key-value pairs. However, these individual values can be of other data types, like integer, string, etc, while the array variable itself will be of the Array data type.

// 5. Arrays

$fruits = array("apple", "orange", "mango");

var_dump($fruits);

$fruit = array(
    "name" => "apple",
    "color" => "red"
); // associative array

var_dump($fruit);

$fruit = [
    "name" => "mango",
    "color" => "yellow"
];

var_dump($fruit);

6. Object

Instances of a class will have the Object data type. For instance, consider the Camera class below. $myCamera and $yourCamera have the Object data type.

// 6. Object

class Camera {
    public $maker;

    public function __construct($maker) {
        $this->maker = $maker;
    }

    public function getMaker() {
        return $maker;
    }
}

$myCamera = new Camera('Nikon');
var_dump($myCamera);

$yourCamera = new Camera('Canon');
var_dump($yourCamera);

7. Iterables

You can use the iterable keyword to denote that a function receives an iterable value as its parameter. Otherwise, PHP throws an error.

You can use the foreach loop to go through an iterable, which can be an array or an object. Iterable is a pseudo data type.

// 7. Iterables

$fruits = array("apple", "orange", "mango");

// $fruits = "apple and mango";

function getFruits(iterable $iterable) {
    // var_dump($iterable);
    foreach ($iterable as $key => $value) {
        echo $key+1 . ") " . $value;
        echo "<br>";
    }
}

getFruits($fruits);

8. Callable

Sometimes, you may need to pass the name of the function dynamically using a string variable. So, if there is a function that accepts a function name as a parameter, then you can type hint it using the callable keyword. In case there isn’t any function existing in the name of that passed variable, PHP will throw an error.

// 8. Callable

function printHello() {
    echo "Hello";
}

function callFn(callable $fn) {
    $fn();
}

$functionName = "printHello";
// $functionName = "sayHello";

callFn($functionName);

9. Null

Null is a special data type. As the name denotes, it holds no value.

// 9. Null - no value data type

$nothing = NULL;

var_dump($nothing);

10. Resource

Resource is another special data type in PHP. An example is a file handler variable, which is used to read, write, and append data to the files on the system.

<?php

// 10. Resource

$handle = fopen("some.txt", "w");

var_dump($handle);