PHP | Data Types

Values & Types | PHP Guide

In PHP, Data types are used to declare one particular type of data as in all programming languages. Data Types define the type of data a variable can store.
PHP supports 8 primitive data types that can be categorized further in 3 types:

  1. Scalar Types (predefined)

This particular data types holds only single value. There are 4 scalar data types in PHP.

  • String

A string is a non-numeric data type. It holds letters or any alphabets, numbers, and even special characters. String must be written either within single quotes or in double quotes, both acceptable.

  • Integers

Integers hold only whole numbers including positive and negative numbers.

Rules for integers:

An integer must have at least one digit.

An integer must not have a decimal point.

An integer can be either positive or negative.

  • Float

Float is a number with a decimal point or a number in exponential form. It can hold numbers with a fractional or decimal point, including a negative or positive sign.

  • Booleans

Booleans are one of the simplest data type in PHP. It hold only 2 values TRUE and FALSE. It is mainly used in conditional statements.

2. Compound Types

This particular data types holds multiple values. There are 2 compound data types in PHP.

  • Array

An array is a compound data type that can store multiple values with same data type in a single declared variable.

  • Objects

Objects are the instances of user-defined classes that can hold both values and functions and information for data processing specific to the class.

Objects are declared and created from the new keyword.  

3. Special Types

There are 2 special data types in PHP.

  • Null

These are special types of variables that can hold only one value NULL, which is intentional declared to be null. There is a convention of writing it in capital letters as it is case sensitive written in capital letters.

  • Resources

Resources in PHP are not an exact data type. These are basically used to store references to some function call or to external PHP resources. This is an advanced topic of PHP.

Let’s take an example for all above data types:

Summary

After reading this blog, you should have a better clear concept of some of the major data types that are available in PHP. Each of these data types will become important with a particular role as you develop programming projects in the PHP language.

I hope you find this particular blog helpful and informative about data types in PHP.

Keep practicing and exploring, there are lot more to know.

Thank you!!