PHP Data Types at A Glance

PHP supports specific data types. You can’t just use anything within PHP scripts. In this post, I will discuss the data types which PHP supports and you can use.

Basically PHP supports eight types of data. These data types are categorized into Scalar types, Compound types and special types.

First, let’s check the Scalar types. There are four scalar types of data in PHP.

  1. boolean
  2. integer
  3. float
  4. string

Boolean – Boolean data type represents truth value. It can be either TRUE or FALSE. Booleans are often used in conditional testing.

Integer – Integers are non-decimal numbers between -2,147,483,648 and 2,147,483,647. Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +).

Float – Floats are used for numbers that have fractional components or numbers with decimal points. Floats can contain both positive and negative values.

String – Strings are basically series of characters. Any text, put in a variable is a string.

Now let’s check these data types with some examples.

Now, let’s check out the Compound types. There are two types of compound data types in PHP.

  1. Arrays
  2. Objects

Arrays – Arrays can store multiple multiple values in one variable. Arrays also acts like a ordered map for values when we associate values to keys. 

Objects – An object stores data and functionalities on how to process that data. Object is created by instantiating a class. First, a class is declared than the object is created by instantiating that class.

Let’s check out an example of arrays and objects.

Notice how one variable is containing multiple values. var_dump() is used to output the array values. Then we created a class and created an object by instantiating that class.

Th other type is Special Type. There are two special types of data in PHP.

  1. NULL
  2. Resources

NULL – This data type can have only one value : NULL. It represents a variable with no value. If you create a variable without assigning any values to that, it is automatically assigned to a value of NULL. You can also set the value to NULL.

Resources – Resource is used for variables that hold data or reference of an external resource. This is basically not a data type. For example, when we use database call in our PHP that’s a resource data type. Check this list of resources you can use in PHP.

So, these were the PHP data types at glance. Let me know through comments if I am missing anything.

Click Here to Leave a Comment Below

Leave a Reply: