PHP Type Juggling (original) (raw)

Skip to content

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 string`Code 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 integer`Code 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";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>l</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">total=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>total+qty = "20"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>l</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">total = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">o</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>total + qty="20";<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>l</mi><mo>=</mo></mrow><annotationencoding="application/xtex">total=</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="marginright:0.01968em;">l</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>total+qty;

echo $total; // 120`Code 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="20pieces";<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>l</mi><mo>=</mo></mrow><annotationencoding="application/x−tex">total=</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>total+qty = "20 pieces"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>l</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">total = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">o</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>total + qty="20pieces";<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>o</mi><mi>t</mi><mi>a</mi><mi>l</mi><mo>=</mo></mrow><annotationencoding="application/xtex">total=</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6944em;"></span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">a</span><spanclass="mordmathnormal"style="marginright:0.01968em;">l</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span></span></span></span>total+qty;

echo $total; // 120`Code 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 #

Did you find this tutorial useful?