PHP Data Types

Summary: in this tutorial, you will learn about PHP data types including scalar types, compound types, and special types.

Introduction to PHP data types #

A type specifies the amount of memory allocated to a value associated with it. A type also determines the operations that you can perform on it.

PHP has ten primitive types including four scalar types, four compound types, and two special types:

PHP Types

Scalar types

Compound types

Special types

Scalar types #

A variable is a scalar when it holds a single value of the type integer, float, string, or boolean.

Integer #

Integers are whole numbers defined in the set {…-3,-2-,-1,0,1,2,3…}.  The size of the integer depends on the platform where PHP runs.

The constant PHP_INT_SIZE specifies the size of the integer on a specific platform. PHP uses the int keyword to denote the integer type.

The following example illustrates some integers:

<?php

$count = 0;
$max = 1000;
$page_size = 10;

echo $count . '<br>';
echo $max . '<br>';
echo $page_size  . '<br>';Code language: PHP (php)

Try it

Float #

Floats are floating-point numbers, which are also known as floats, doubles, or real numbers.

PHP uses the IEEE 754 double format to represent floats, which, like other programming languages, have limited precision.

PHP uses the float keyword to represent the floating-point numbers. The following example illustrates the floating-point numbers in PHP:

<?php

$price = 10.25;
$tax = 0.08;

echo $price * (1  + $tax);Code language: PHP (php)

Try it

Boolean #

Boolean represents a truth value that can be either true or false. PHP uses the bool keyword to represent the Boolean type.

The bool type has two values true and false. Since keywords are case-insensitive, you can use true, True, TRUE, false, False, and False to indicate boolean values.

The following example shows how to assign Boolean values to variables:

<?php

$is_admin = true;
$is_user_logged_in = false;Code language: PHP (php)

When you use the values of other types in the boolean context, such as if-else and switch-case statements, PHP converts them to the boolean values.

PHP treats the following values as false:

  • The false keyword.
  • The integer 0 and -0 (zero).
  • The floats 0.0 and -0.0 (zero).
  • The empty string ("", '') and the string “0”.
  • The empty array (array() or []).
  • The null.
  • The SimpleXML objects created from attributeless empty elements.

The values that are not one of these falsy values above are true.

String #

A string is a sequence of characters surrounded by single quotes (‘) or double quotes (“). For example:

<?php

$str = 'PHP scalar type';
echo $str;

$message = "PHP data types";
echo $message;Code language: PHP (php)

Compound types #

Compound data includes values that contain more than one value. PHP has two compound types, array and object.

Array #

An array is an ordered map that associates keys with values. For example, you can define a list of items in a shopping cart like this:

<?php

$carts = [ 'laptop', 'mouse', 'keyboard' ];Code language: PHP (php)

The $carts array contains three string values. It maps the index 0, 1, and 2 to the values 'laptop', 'mouse', and 'keyboard'. The $carts is called an indexed array because it uses numeric indexes as keys.

To access a value in an array, you use the square brackets:

<?php

$carts = [ 'laptop', 'mouse', 'keyboard' ];

echo $carts[0] .'<br>'; // 'laptop'
echo $carts[1] .'<br>'; // 'mouse'
echo $carts[2] .'<br>'; // 'keyboard'Code language: PHP (php)

Try it

Besides numeric indexes, you can use strings as keys for the array elements. These arrays are known as associative arrays. For example:

<?php

$prices = [
   'laptop' => 1000,
   'mouse' => 50,
   'keyboard' => 120
];Code language: PHP (php)

To access an element in an associative array, you specify the key in the square brackets. For example:

<?php

$prices = [
   'laptop' => 1000,
   'mouse' => 50,
   'keyboard' => 120
];

echo $prices['laptop'] . '<br>'; // 1000
echo $prices['mouse'] . '<br>'; // 50
echo $prices['keyboard'] . '<br>'; // 120Code language: PHP (php)

Try it

Object #

An object is an instance of a class. It’s a central concept in object-oriented programming.

An object has properties. For example, a person object may have a first name, last name, and age properties.

An object also has behaviors, which are known as methods. For example, a person object can have a method called getFullName() that returns the full name.

To learn more about objects, check out the object tutorial.

Special types #

PHP has two special types: null and resource

Null #

The null type has one value called null that represents a variable with no value.

Resource #

A resource is a special variable that references to an external resource such as:

php resources

A resource is not actual data like a string or number. Instead, it’s a reference to something outside of PHP. PHP uses resources to manage things outside of PHP efficiently by automatically free it when it is no longer in use.

Summary #

  • PHP has four scalar types, four compound types, and two special types.
  • Scale types: integer, float, string, and boolean.
  • Compound types: array and object.
  • Special types: null and resource.
Did you find this tutorial useful?