Data Types in PHP

Learn via video courses
Topics Covered

Overview

PHP is a dynamic, loosely-typed programming language that supports several data types. The data type is a fundamental aspect of a programming language, defining what kind of data can be stored and manipulated in a variable or expression.

In PHP, there are several built-in data types, including integers, floating-point numbers, strings, Booleans, arrays, objects, and NULL. These data types are used to store and manipulate different types of data in variables and expressions.

Introduction

Data types are an essential part of any programming language, including PHP. They define the type of data that can be stored in a variable or used in an expression. PHP is a dynamic, loosely-typed language, which means that variables do not need to be declared with a specific data type. However, it is still important to understand the various data types in PHP to avoid unexpected results and ensure efficient code.

Now let us read about PHP Data Types: Scalar Types.

PHP Data Types: Scalar Types

In PHP, scalar types are those data types that can hold only one value at a time. They are called scalar because they represent a single value, as opposed to compound types that can store multiple values. PHP has four scalar data types: integers, floating-point numbers, strings, and Booleans. Each of these data types has its subtypes, which we will explain below.

  1. Integer:
    An integer is a whole number without a decimal point. In PHP, integers can be represented in decimal, hexadecimal, and octal format.

    • Signed integers:
      These are integers that can be both positive and negative. They are represented in PHP using the 'int' or 'integer' keyword.

    • Hexadecimal integers:
      These are integers that are represented in base-16. They are prefixed with '0x' or '0X' and can contain the digits 090-9 and the letters AFA-F.

      Example:

  2. Float:
    A float or floating-point number is a number with a decimal point or an exponent.

    Here in this code also the data types of the float variables are mentioned beside them only.

  3. String:
    A string is a sequence of characters enclosed in quotes. In PHP, strings can be enclosed in single or double quotes.

    The above is an example of a single-quoted string in php.

    • Double-quoted strings:
      These are strings enclosed in double quotes. They are slower than single-quoted strings but can parse more escape sequences and variable values.

    The above code represents double-quoted strings in php.

  4. Boolean:
    A boolean represents a value of true or false. In PHP, the value of true is represented by 1, and the value of false is represented by 0. Here are some examples of booleans in PHP:

PHP Data Types: Compound Types

In PHP, compound data types are used to store collections of data, including arrays and objects.

  1. Arrays:
    An array is a collection of values that can be accessed using an index or key. PHP supports two types of arrays: indexed and associative.

  2. Objects:
    An object is a user-defined data type that stores data and behavior. Objects are created from classes, which define the properties and methods of the object. Here is an example of how to create and use an object:

    In the above example, we define a class called "Person" with two properties and one method. We then create an object of that class, set its properties, and call its method.

  3. Callable:
    A callable is a data type that represents a function or method that can be called. Callables can be used as arguments to other functions or as values in arrays. Here is an example of using a callable as a callback function:

    In the above example, we define a callback function and a function that takes a callback as an argument. We then pass the callback function as an argument to the function and call it with a string argument.

Now let us read about special types in php:

PHP Data Types: Special Types

PHP is a dynamically-typed programming language, which means that the data types of variables are determined at runtime based on the value assigned to them.

1. NULL Type

The NULL type represents a variable that has no value or has been explicitly set to NULL. It is often used to initialize variables or to indicate that a value is missing.

For example:

2. Resource Type

Resource types are used to represent external resources that are managed by PHP. These resources could be anything from database connections to file handles, and they are represented by a special data type called "resource".

In this example, we are creating a file handle using the fopen() function. The handle is then stored in the $handle variable, which is of type resource.

3. Callable Type

The callable type is used to represent functions or methods that can be called regular functions. It is used in situations where a function or method needs to be passed as an argument to another function or method.

For example:

4. Iterable Type

The iterable type was introduced in PHP 7.1 and is used to represent objects that can be iterated over using a foreach loop. It includes arrays, as well as objects that implement the Traversable interface.

For example:

5. Object Types

Object types are used to represent instances of classes. This data type is used extensively in object-oriented programming, which is a programming paradigm that focuses on creating reusable code in the form of classes and objects.

Here's an example:

Examples

Boolean

A boolean variable can only have two possible values - true or false. It is often used in conditional statements to control program flow.

For example:

In this example, we are defining a boolean variable $is_active and using it in an if statement to check whether the user is active or not.

Integer

An integer variable represents a whole number without a fractional part. It can be positive, negative, or zero.

For example:

In this example, we are defining an integer variable $age and using it to output the person's age.

Double

A double variable represents a floating-point number with a decimal point. It can be positive, negative, or zero.

For example:

In this example, we are defining a double variable $price and using it to output the price of an item.

String

A string variable represents a sequence of characters. It can contain letters, numbers, and special characters.

For example:

In this example, we are defining a string variable $name and using it to output a greeting.

Resource

A resource variable represents an external resource that is managed by PHP. Examples of resources include database connections, file handles, and network sockets.

For example:

In this example, we are creating a file handle using the fopen() function and storing it in a resource variable $handle.

Composite Data Types

Composite data types in PHP are used to store and manipulate complex data structures that contain multiple values or data types.

1. Arrays

An array is a composite data type that stores a collection of values. Arrays can be indexed by integers or strings and can contain values of any data type.

Here are some examples:

Numeric Indexed Array:

This creates an array that contains five elements with numeric keys. The values are 1, 2, 3, 4, and 5.

Associative Array:

This creates an associative array that contains three elements with string keys. The keys are'name', 'age', and 'city', and their corresponding values are 'John', 30, and 'New York'.

Objects

An object is a composite data type that represents an instance of a class. Objects contain properties and methods, which are used to store and manipulate data. Here are some examples:

Simple Object:

This creates an instance of the Person class and sets the name, age, and city properties. The sayHello() method is then called, which outputs "Hello, my name is John and I am 30 years old".

Complex Object:

Output:

Conclusion

  • PHP supports a wide range of data types, including scalar types (such as integers, floats, and strings), Boolean types, special types (such as NULL), and composite types (such as arrays and objects).
  • Scalar types in PHP are used to represent single values, while composite types are used to represent collections of values.
  • PHP is a dynamically typed language, which means that the data type of a variable can change at runtime based on the value that it holds.