PHP Type Juggling

Summary: in this tutorial, you’ll learn about PHP type juggling and how it works.

Introduction to PHP type juggling #

PHP is a loosely typed programming language. It means that when you define a variable, you don’t need to declare its type. Internally, PHP will determine the type by the context in which you use the variable.

For example, if you assign a string to a variable, its type will be the string:

<?php

$my_var = 'PHP'; // a stringCode language: PHP (php)

Try it

If you assign an integer to the same variable, and its type will be the integer:

<?php

$my_var = 'PHP'; // a string
$my_var = 100; // an integerCode language: PHP (php)

Try it

PHP has a feature called type juggling. It means that when comparing variables of different types, PHP will convert them to the common, comparable type. For example:

<?php
$qty = 20;
if($qty == '20') {
    echo 'Equal';
}Code language: PHP (php)

Try it

Output:

EqualCode language: PHP (php)

Because of type juggling, PHP converts the string '20' to an integer (20) and compares it with the $qty variable. The result is true. Therefore, you’ll see the message Equal in the output.

The type juggling also works in arithmetic operations for variables of different types. The following example illustrates how the type juggling works in an arithmetic operation:

<?php

$total = 100;
$qty = "20";
$total = $total + $qty;

echo $total; // 120Code language: PHP (php)

Try it

The type of $total is an integer, whereas the $qty is a string. To calculate the sum, PHP first converts the value of the $qty variable to an integer. The result is an integer.

Consider the following example:

<?php

$total = 100;
$qty = "20 pieces";
$total = $total + $qty;

echo $total; // 120Code language: PHP (php)

Try it

In this example, PHP casts the string “20 pieces” as an integer 20 before calculating the sum.

Note that PHP also throws a warning message:

A non-numeric value encountered

Summary #

  • PHP is a loosely typed language. PHP determines the variable type based on the value.
  • When comparing values of different types, PHP implicitly converts them to the same comparable type.
Did you find this tutorial useful?