A look into PHP implicit casting

PHP Implicit Casting

PHP implicit casting is very common. But before we see what it mean, let’s see why implicit casting happens.

Again, PHP is supper-friendly. Also, it is a loosely typed language. In other words, PHP allows us to declare a variable and simply use it. And, PHP determines the data type of the variable automatically.

Note: PHP determines the data type of variables automatically. We just declare a variable and assign it a value. PHP determines the type from the value.

Furthermore, PHP continuously and automatically converts types from one to another when deemed necessary. We call this implicit casting or Type Juggling.

Okey, But why is PHP juggling type?

Because it is always trying to make sense of our code. For instance, if an operation results in a number that is larger than the integer range. PHP implicitly casts it into a floating-point number. Otherwise, we might accidentally truncate and lose some values if it wasn’t for this implicit casting.

However, there are some scenarios where PHP automatic type juggling is not as obvious. Here are few examples:-

1. PHP Implicit Casting During Arithmetic Operations

For instance, take a look at this simple code. Expectedly, most programming languages might complain. But, PHP tries its best. Most of the time, operations involving a number and a string containing a number computes to another number.

$x = 3 + "10% discount";
echo $x;// We will get 13, a number. With some notice about format of our number.

This is true as long as the first operand is a number and the second operand is a string starting with a number. The string can be followed by any character. But, if it starts with numbers, PHP casts it into that number.

As a result, PHP casts the second operand in the above snippet into 10. Which is an integer. Hence, the result of the operation becomes 13.

Similarly, the following snippet will also result in 13.

$x = 3 + "10 items";
echo $x;

Why? Because PHP implicitly casts "10 items" into 10, the number.

Yet, this rule turns upside down. Especially, when we have a string starting with characters followed by a number as a second operand. In this situation, PHP implicitly casts the second operand into a null.

$x = 3 + "Items = 10";
echo $x; // We get 3. Also, A warning of non-numeric value is displayed.

To sum up, PHP interpretation of a string during arithmetic operations with a number is one of the following:-

  1. As another number. As long as the string starts with a number.
  2. Or as null. If the string starts with a character.

Plus, In any of the above cases, if the number is a floating-point, PHP casts the other operand and the result as a floating-point number. For example:-

$x = 3 + "2.3$";//Will result in 5.3 

Additionally, we don’t have to worry about truncating a number value when we divide integers. Unlike other languages, PHP casts the division value into floating-point numbers and preserves the value.

$x = 2/3; //Results in a floating point in 0.66666667

Again, PHP is implicitly converting the type into a floating-point number.

PHP Implicit Casting During Comparison Operators

Similar to arithmetic operations, PHP performs some castings during comparison operations. For instance:-

$a = '2';
$b = 2;
if ($a == $b) {
  echo "They are equals. Because $a is cast into number";
}

As you can see, PHP implicitly casting is at full play during the equality comparison. As a result, the operator treats $a as an integer to compare with $b.

Even if the two operands are strings with just a number in them, The operation casts and compares them as numbers. However, if one of them is a string that is not entirely numeric the operation will compare them lexicographically.

Note: there is a comparison operator that also checks for type and it is called identity operator (===). We can use the identity operator to safely compare value and avoid casting.

Type Juggling during Concatenation

First, let’s define what concatenation means. The concatenation operator ( . ) appends the right and left operands together and returns a new string. If either or all of the operands of the concatenation operator are non-string, PHP will implicitly cast them into one. For instance:-

$n = 3;
$message = "There are".$n."items";//$n is implicitly cast from integer into string.

Conclusion

Finally, just remember this. PHP tries to make sense of our code. In the process, it converts one type into another. We call this type juggling or implicit casting.

In short, type juggling can be a major source of confusion. But we can avoid it following the above simple rules.

Finally, note that we can take full control of typecasting by manually converting one type into another. We call this type of casting explicit typecasting. The official PHP documentation goes into detail regarding explicit typecasting.

This blog is part of a relearn PHP series where I am blogging every week on one basic PHP topic. Let me know if you like it.

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

Learn Modern PHP

Subscribe to learn modern PHP in the most simple way.