PHP Data Types Cheat Sheet

Updated: January 9, 2024 By: Guest Contributor Post a comment

Introduction

PHP is a flexible language with a simple type system. Understanding its basic to advanced data types is key to writing effective and powerful PHP code. This cheat sheet will serve as a quick reference and guide through the varied data types in PHP, illustrated with code examples.

Basic PHP Data Types

PHP supports four scalar data types:

  • Boolean: This type only has two values: true or false.
  • Integer: Non-decimal number between PHP_INT_MIN and PHP_INT_MAX.
  • Float: A number with a decimal point or a number in exponential form (e.g., 1.234 or 4E-10).
  • String: A sequence of characters, can be specified using single quotes or double quotes.

Below are examples showcasing the declarations of basic data types in PHP:

<?php
$boolean = TRUE; // A boolean
$integer = 42; // An integer
$float = 3.14; // A float
$string = 'Hello, World!'; // A string
?>

Compound Data Types

PHP also includes two compound data types:

  • Array: An ordered map of values. Arrays can hold other arrays, creating multidimensional arrays.
  • Object: An instance of a class, capable of holding attributes and methods.

Below are examples of array and object declarations:

<?php
// Indexed array
$array = array('Apple', 'Banana', 'Cherry');

// Associative array
$assocArray = array('color' => 'red', 'taste' => 'sweet');

// Object creation using stdClass
$object = new stdClass();
$object->name = 'John';
$object->age = 25;
?>

Special Data Types

PHP also has two special data types:

  • Resource: A special variable holding a reference to an external resource.
  • NULL: A variable that has no value assigned to it.

Below is an example of resource and NULL data types:

<?php
// Resource 
$resource = fopen('somefile.txt', 'r');

// NULL
$nullVar = NULL;
?>

Type Juggling and Casting

PHP will automatically convert data types where appropriate, a feature known as type juggling. However, explicit type casting is also possible. Below are examples of both:

<?php
// Type Juggling
$sum = '3' + 4; // $sum will be an integer (7)

// Type Casting
$sum = (int) '3' + 4; // $sum will be an integer (7)
$bool = (bool) 1; // $bool will be a boolean (true)
?>

Advanced Topics in PHP Data Types

In more advanced applications, understanding constant data types, the stdClass, and the iterable pseudo data type is beneficial. Constants allow specifying an unchangeable value. stdClass is the default object class for objects that are not declared (cast) as objects. Iterable is a pseudo data type introduced in PHP 7.1, which allows for interacting over a data set.

<?php
// Constants
define('MY_CONSTANT', 123);

// stdClass objects
$user = new stdClass();
$user->name = 'Alice';

// Iterable
function printIterable(iterable $myIterable) {
  foreach ($myIterable as $item) {
    echo $item;
  }
}

// Example usage
$myArray = ['A', 'B', 'C'];
printIterable($myArray);
?>

Conclusion

This PHP data types cheat sheet is intended to get you quickly up to speed with PHP’s core data types. As a dynamic language, PHP can be easy to pick up, but a solid understanding of the underlying data types will greatly improve your code’s clarity and efficiency.