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;

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;

$score2 = -25;

var_dump($score1 + $score2);

3. Floats

The float data type can contain numbers with decimal places.

// 3. Floats - decimal numbers

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

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
    if(true) {
        echo "hello world!";
        echo "second line";
        if(true) {
            echo "third line";
echo $codeBlock;

$codeBlock = <<< 'NOW'
    $a = true;
    if($a) {
        echo "hello world!";
        echo "second line";
        if(true) {
            echo "third line";
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");


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


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


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');

$yourCamera = new Camera('Canon');

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>";


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) {

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


9. Null

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

// 9. Null - no value data type

$nothing = NULL;


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.


// 10. Resource

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