In this article, you will understand what is PHP type juggling and how it does work. But before you do that, you have to learn more about PHP data types.

Basically, During the program translation, there are some defined variables with the data type and these data types are already changed to another data type through the program compilation. This process has a name called PHP type juggling.

Also, this concept is referring to the arithmetic that is already written in the first letter of a string data type value and calculated with another integer data type value.

Let’s see how it works with PHP code examples.

PHP Type Juggling during the Program Compilation

As I mentioned before, the type of juggling is happening during the program translation. The following example shows you the first data type for the PHP variable was an integer value.

<?php 
  $value = 150;
  echo gettype($value); // integer
?>

But during the program execution, this data type is changed to a PHP array.

<?php 
  $value = 150;
  $value = array(150);
  echo gettype( $value ); // array
?>

So, that is maybe happening in the program execution to change the variable value into another or assign a new boolean type or whatever to be proper for some operations in the program stack. Such as storing new data in the same variable, changing the old value to another, and so on.

In the following section, I am going to explain what is happening in the arithmetic operations during the compilation if the string value is calculated with an integer value.

PHP Type Juggling with Comparison Operators

The PHP type juggling has two comparison modes, as the following list.

  • The strict operators (===).
  • The loose operators (==).

The first one is for detecting value and type but on another hand, the second one makes the checking easier by only checking for the value. Let’s see that.

In the next example, I will compare a string variable with another integer data type using the loose comparison.

<?php
  $string  = "105";
  $integer = 105;
  
  if ( $string == $integer ) {
    echo "Correct Result";
  } else {
    echo "Not Correct !";
  }
?>

The output of this example would be – “Correct Result”.

Entirely, the PHP automatically converts one of the two variables to be the same data type as the other one. So, as you saw in the previous example, we have a variable with a string data type and another one with an integer data type.

In the program compilation, it starts the casting automatically to convert the string value to an integer data type and once it compared the two values it will give you identical true values. Also, this process is called PHP-type juggling.

In the following section, I am going to discuss how the arithmetic operator works behind the scene under the cover of PHP-type juggling.

PHP Type Juggling for the Arithmetic Operations

In the previous section, we explained the automatically converting during the compilation. And here we have the same concept in the arithmetic operations.

For a quick example.

<?php
  $string  = "10";
  $integer =  100;
  $calcs   = $string * $integer;
  echo $calcs;
  echo "\n";
  echo gettype($calcs);
?>

The output would be like the below.

1000
integer

In the next example, we will use characters with numbers and calculate them with another integer value.

<?php
  $calc_1 = "hello105" + 15;
  echo $calc_1; // 15
?>

The PHP type juggling here, converted the string value to an integer like this (int) “hello105” and the casting result for it was “0“. Because it contains characters with numerals. If you already didn’t knew the reason, just do a quick revision for the PHP Type Casting tutorial.

But what about if we made minor changes in the string to be like this 105hello”. So the numerical data is at the beginning position of the string. Let’s see that in the PHP code.

<?php
  $calc_1 = "105hello" + 15;
  echo $calc_1; // 120
?>

The result would be “120”. And that happened because the first impression of this string was an integer value inside a string.

It converted the numbers inside the strings to integers and letters to no value, so the final casting was “105” not 105hello”.

Wrapping Up

In this article, you learned what is the PHP type juggling and how it works with the other types behind the scene. Also, you saw examples for the loose comparison mode. And how that affects on the old value during the compilation if it has another data type.