Skip to content

PHP Tutorial Data Types

Type Juggling

Lesson 45 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

PHP converts types automatically when an operation needs it. This is called type juggling. It is convenient, and it is also a classic source of bugs.

Numbers inside strings

index.php
PHP
<?php
var_dump("5" + "5");
var_dump("5" . "5");
var_dump("2.5" * 2);
Output
OUTPUT
int(10)
string(2) "55"
float(5)

The + operator wants numbers, so PHP converts the strings. The . operator wants strings, so it joins them.

Strings that only start like a number

index.php
PHP
<?php
var_dump("5 apples" + 1);
Output
OUTPUT
Warning: A non-numeric value encountered in /path/to/index.php on line 2
int(6)

Strings that are not numeric at all

Since PHP 8, arithmetic on a string that is not a number throws an error.

index.php
PHP
<?php
try {
    echo "abc" + 1;
} catch (TypeError $e) {
    echo $e->getMessage();
}
Output
OUTPUT
Unsupported operand types: string + int

Loose comparison

The == operator also juggles types, and PHP 8 made these rules saner.

index.php
PHP
<?php
var_dump(0 == "a");        // false in PHP 8, was true in PHP 7
var_dump("1" == "01");     // both numeric strings, so compared as numbers
var_dump(100 == "1e2");    // "1e2" is the number 100
var_dump(null == false);
Output
OUTPUT
bool(false)
bool(true)
bool(true)
bool(true)

Why this is dangerous

A loose comparison can let a wrong value through.

index.php
PHP
<?php
$role = "0";

if ($role == false) {
    echo "Treated as no role";
}
Output
OUTPUT
Treated as no role

The safe habit

  • Use === and !== instead of == and !=. See Equality vs Identity.
  • Turn on strict typing in your files.
  • Convert on purpose with a cast, so the next person sees what you meant.
lightbulb
Rule of thumb
If you are not sure whether PHP will convert something, do not rely on it. Convert it yourself and write the type you expect.