A Look into PHP Data Types.

A look into PHP data Types

PHP provides three groups of data types. The first group is scalar, which is used for single values. And the second is composite, which is a group of scalars. Finally, we have a special group.

Let’s see them one by one.

  1. Scalar PHP Data Types
    1. Integer numbers
    2. Floating Point numbers
    3. Strings
    4. Booleans
  2. Composite PHP Data Types
    1. Array
    2. Object
  3. Special PHP Data Types
    1. Null
    2. Resources.

Scalar PHP Data Types

Scalar PHP data types represent single values in our program. For example, the number 8 is independent of other values. For these situations, we have four types.

Integers

The first scalar type we will see is Integers. Integers represent whole numbers, such as 1, 12, 212, etc.

We call these values literals.

Beware. In PHP, integer literals can be written in decimal, octal, binary, or hexadecimal.

It is good practice to check for a type of variable. In PHP, we can check if a variable is an integer or not using the is_int or is_integer functions.

if(is_int($x)) {
  //$x is an integer and we can perform integer operations here.
}

Floating-Point Numbers

In real life, we are always dealing with real numbers with decimal places. PHP gives us float for working them. But, working with float is tricky and we have to be very careful. For instance, when checking two floating-point numbers we should follow this practice.

if(intval($a * 100) == intval($b*100)){
  //Number is equl to two decimal places.
}

Just like integers, we can use the is_float or is_real functions to check if a given variable is a float or not.

Strings

A string is a group of one or more characters. ‘A’ is a string/character, when it comes together with other letters, it definitely becomes a string. Example, ‘At’.

In programming, Strings play a crucial role. That is because they are very common. For instance, this blog is made up of strings.

PHP gives good focus and core-level support to work with strings. To start with, we can use single or double quotes to represent string literals. Both work similarly, with few differences. In single quotes, interpolation and escape sequences don’t work. Whereas, double quote strings support both escape characters and double quotes.

$name = "Alula";
echo "Good morning $name"
echo 'Good morning $name'

Just like an integer and float, Strings have a way to check if a variable is actually a string or not. As you have probably guessed, it is is_string().

Boolean

The fourth data type from the first group is Boolean. A boolean represents either true or false. Hence, unlike the other types, it can only have two possible values.

The Tricky Part About PHP and False

There is some ticky situation with PHP boolean type that we have to be careful of. And that is PHPs attempt to categorize some data types as true or false. Here are some Falsy types.

  • false itself. No problem here.
  • The integer value 0. 
  • The floating-point value 0.0, 
  • The empty string “” and the string “0”
  • An empty array []
  • The NULL value.

For me, this is where PHP has been a source of pain. In its desperate attempt to help, PHP assumes all these values as falsy. That just leads to disastrous.

Anyways, back to the topic.

PHP assumes anything other than that group as true. And just like other types, we have the is_bool function to check if a function is boolean or not.

The Composite PHP Data Types

These are a group of data types that bring together the scaler data types together.

Arrays are a group of similar scalar types identified by either an index or a key.

Aside Note: I have written an article on PHP array functions, please give it a quick read.

On the other hand, Objects are instances of a class that represent that entity. For example, this blog can be represented as a group than a separate title, tag, category. An object is a comprehensive representation than all the scaler types.

Anyways, both arrays and group demand their good entry. Hope I will jot down my notes on them.

Your subscription could not be saved. Please try again.
Your subscription has been successful.

Learn Modern JS, CSS, & PHP

Subscribe to learn modern PHP in the most simple way.