Data Types in PHP

I. Data types

There are different data types in PHP.

  • 4 Scalar types
    • bool: true/ false
    • int: 0, 1, -1 (no decimal)
    • float: -2.5, 0.001, 4.56
    • string: “Hello World”
  • 4 Compound types
    • array: list of items
    • object
    • callable
    • iterable
  • 2 Special types
    • resource
    • null

II. Check data types

We can use var_dump() to check a type and value of a variable.

$complete = true;
var_dump($complete); // bool(true)

III. Strict type

We can use strict type to get a better quality of code and know what type is expected.

declare(strict_types=1);

function sum(float $x, float $y) {
    return $x + $y
}

$sum = sum(1,2);

echo $sum; // 3
var_dump($sum); // float(3)