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 that allocates 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 scala 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;
Code language: HTML, XML (xml)

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. Like other programming languages, floats 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;
Code language: HTML, XML (xml)

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: HTML, XML (xml)

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'; $message = "PHP data types";
Code language: HTML, XML (xml)

Compound types

Compound data includes the values that contain more than one value. PHP has two compound types including 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: HTML, XML (xml)

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 an value in an array, you use the square brackets:

<?php echo $carts[0]; // 'laptop' echo $carts[1]; // 'mouse' echo $carts[2]; // 'keyboard'
Code language: HTML, XML (xml)

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: HTML, XML (xml)

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

<?php echo $prices['laptop']; // 1000 echo $prices['mouse']; // 50 echo $prices['keyboard']; // 120
Code language: HTML, XML (xml)

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 the 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 one value called null that represents a variable with no value.

Resource

The resource type holds a reference to an external resource, e.g. a file handle or a database connection.

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?