PHP Data Types Tutorial

In this tutorial we learn about scalar, compound and special data types in PHP like integers, arrays, and null. We cover booleans, integers, floats, strings and null.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

What are data types

When we think about data in the real world, we often group it into different types. If we ask a young student in school what math is, they will most likely reply that it’s a bunch of numbers, and they would be (mostly) correct.

In programming we also sort different data into types, such as numbers, words, objects etc. PHP supports 8 different types of data for storage, which are categorized into 3 main types.

  1. Scalar types: integer, float, string and boolean
  2. Compound types: arrays and objects
  3. Special types: resource and null

In this tutorial lesson we will cover scalar types and the special type NULL.

Boolean

A boolean data type in programming is the same as a boolean type in methematics. A bool can hold only two values, either true or false.

The values of true and false are keywords in PHP that represent 1 and 0 respectively and they help us to write code with logical conditions.

When writing a boolean, we only specify the keywords true or false in all lowercase letters.

Example: bool
<?php

$yes = true;
$no = false;

?>

Typically, we would use booleans in conditional logic, like if something is true, execute some code etc.

We cover conditional logic in the conditional Control FlowOpens up in a new page tutorial lesson but here is a quick example to see how it would work.

Example: conditional bool
<?php

$isBirthday = true;

if($isBirthday) {
    echo "Happy birthday!";
} else {
    echo "No cake for you...";
}

?>

Integer

The integer, or int, is a non-decimal number between -2 billion and 2 billion, specifically -2,147,483,648 to 2,147,483,647.

The value of an int can be positive or negative, but it cannot have a decimal point or it will be interpreted as a float.

Example: int
<?php

$numNeg = -583021;
$numPos = 203;

?>

Float

The float, or floating point number, is a number with a decimal point. A float can also be positive or negative.

Example: float
<?php

const PI = 3.14;
$price = 79.95;

?>

A float can also be in exponential form.

Example: exponential float
<?php

$a = 1.234;
$b = 1.2e3;

?>

The precision of a float in PHP is platform-dependent but a common value is roughly 14 decimal digits (64 bit IEEE format).

Example: float precision
<?php

$precision = 7.99999999999995

?>

String

A string is a combination of multiple ASCII characters, in sequence, to form words or sentences. Strings are enclosed in either double or single quotes and may contain other code, such as HTML.

Example: string
<?php

const GREETING = "Hello there";
$username = 'MorpheusCoolDude101';

echo GREETING;
echo "<br>";
echo $username;

?>

NULL

We can think of NULL as the absence of a value, an empty slot that is still missing information that does not exist yet.

A variable is considered as null by the interpreter if:

  • It has been assigned the value of NULL
  • It has not been assigned any value yet.
  • It has been unset by the unset() function.

Example: null
<?php

// explicit null assignment
$explicit = NULL;

// declaration without assignment
$noValueYet;

// unset
$msg = "Hello World";
unset($msg);

?>

The null value is a constant and is case-insensitive, it must always be written as all uppercase NULL.

Summary: Points to remember

  • A boolean can only be true (1), or false (0).
  • An int can between -2 billion and 2 billion but cannot have a floating point like 3.14.
  • A float has a decimal point and has a precision of roughly 14 digits, depending on the platform.
  • A string can contain any ASCII character and must be enclosed within either double, or single quotes.
  • NULL is the absence of a value. It does not mean 0.