A detailed look into PHP type casting
Tags: php, webdev, beginners, tutorial
Published: 2021-10-05 04:03:37

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

PHP is supper-friendly. Also, it is a loosely typed language. This means PHP allows us to declare a variable and simply use it, the [data type](https://www.businesstyc.com/a-look-into-php-data-types/) will be automatically determined. 

_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 necessary. This is called _**implicit casting**_ or **Type Juggling**. Why is PHP juggling types? 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 range for an integer, PHP implicitly casts it into a floating-point number. But, there are a number of scenarios where PHP automatic type juggling is not as clear. Here are a few scenarios and examples:- 

## PHP Implicit Casting During Arithmetic Operations 

Take a look at this simple code. Most programming languages might complain, but PHP tries its best. 

      $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. It can be followed by any character. PHP casts the second operand into a number. 

Similarly, the following snippet will also result in 13. 

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

Yet, this rule turns upside down 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. Hence, PHP interpretation of a string during arithmetic operations with a number is one of the following:- 

     1\. Operation with another number. As long as the string starts with a number. 
     2\. Operation with _null_. As long as, the string starts with a character. 

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. That is:- 

      $x = 3 + "2.3$";//Will result in 5.3 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. That operator is called **identity operator (===)** and it should be used to avoid implicit casting. 

## Type Juggling during Concatenation 

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. 

Type juggling can be a major source of confusion. It can be avoided by following the above simple rules. We can also take full control of typecasting by manually converting one type into another. This type of casting is called **explicit typecasting.** The official [PHP documentation goes into detail regarding explicit typecasting](https://www.php.net/manual/en/language.types.type-juggling.php). 

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.