1 min read

PHP Data Types

Variables can store data of different types, and different data types can do different things. PHP supports the following data types:


1. Scalar Types

These types hold a single value.

  • String: A sequence of characters, like "Hello world!".
  • Integer: A non-decimal number between -2,147,483,648 and 2,147,483,647.
  • Float (Double): A number with a decimal point or a number in exponential form.
  • Boolean: Represents two possible states: true or false.
$x = "Hello world!"; // String
$y = 5985;           // Integer
$z = 10.365;         // Float
$isActive = true;    // Boolean

2. Compound Types

These types can hold multiple values.

  • Array: Stores multiple values in one single variable.
  • Object: Instances of user-defined classes that can store both values and functions.
$cars = array("Volvo", "BMW", "Toyota"); // Array

class Car {
  public $color;
  public $model;
  public function __construct($color, $model) {
    $this->color = $color;
    $this->model = $model;
  }
}
$myCar = new Car("black", "Volvo"); // Object

3. Special Types

  • NULL: A special data type which can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned to it.
  • Resource: Not an actual data type. It is the storing of a reference to functions and resources external to PHP (e.g., a database connection).

4. Checking Data Type

The var_dump() function returns the data type and value.

$x = 5;
var_dump($x); // int(5)

programming/php/php programming/php/php-arrays programming/php/php-classes