PHP Type Casting and Type Juggling Explained with Examples for Beginners

When working with variables in PHP, you’ll often need to change one data type to another — for example, converting a string into an integer or a float into a string.

PHP provides two ways to handle such conversions:

  1. Type Casting (Manual conversion)

  2. Type Juggling (Automatic conversion)

Let’s understand both step by step.

Type Casting (Manual Conversion)

Type Casting means manually converting a variable from one data type to another using casting operators.
You can control exactly what data type your variable should become.

Syntax:

 

$newVar = (type) $oldVar;

 

Where type can be one of:

  • (int) or (integer)

  • (bool) or (boolean)

  • (float) or (double) or (real)

  • (string)

  • (array)

  • (object)

  • (unset) (to convert to NULL)

Example 1: Casting String to Integer

<?php
$price = "100";
$priceInt = (int)$price;
var_dump($priceInt);
?>

Output:

 
int(100)

 

Explanation:
Here, the string "100" was manually converted into an integer 100.

Example 2: Casting Float to Integer

<?php
$number = 12.75;
$integerNumber = (int)$number;
echo $integerNumber;
?>

Output:

 
12

Explanation:
The decimal part is removed when a float is cast to an integer.

Example 3: Casting Integer to String

<?php
$age = 25;
$ageStr = (string)$age;
var_dump($ageStr);
?>

Output:

 
string(2) "25"

Explanation:
The integer 25 is now stored as a string.

Example 4: Casting Array to Object

<?php
$arr = array("name" => "John", "age" => 30);
$obj = (object)$arr;
echo $obj->name;
?>

Output:

 
John

Explanation:
The array keys become object properties.

Why Use Type Casting

Type casting is useful when:

  • You want precise control over variable types.

  • You’re performing arithmetic or string operations that need uniform types.

  • You’re validating or sanitizing data (e.g., user input).

Type Juggling (Automatic Conversion)

Type Juggling means automatic type conversion performed by PHP based on the context of an operation.

PHP is a loosely typed language, meaning you don’t have to declare variable types — PHP decides the type automatically depending on usage.

Example 1: Automatic Conversion During Arithmetic

<?php
$x = "10";   // String
$y = 5;      // Integer
$z = $x + $y;
var_dump($z);
?>

Output:

 
int(15)

Explanation:
PHP automatically converts the string "10" into an integer before performing addition.

Example 2: Automatic Conversion During Comparison

<?php
if ("5" == 5) {
    echo "Equal";
}
?>

Output:

 
Equal

Explanation:
Here, PHP converts the string "5" into an integer for comparison.

Example 3: Type Juggling in Boolean Context

<?php
$val = "0";
if ($val) {
    echo "True";
} else {
    echo "False";
}
?>

Output:

 
False

Explanation:
The string "0" is automatically treated as false in a Boolean context.

Be Careful with Type Juggling

Although PHP’s flexibility makes coding easy, it can also cause unexpected results if you’re unaware of automatic conversions.

Example:

<?php
var_dump(0 == "a");  // true (both become 0)
var_dump(0 === "a"); // false (strict comparison)
?>

Tip:
Use === (strict equality) to compare both value and type to avoid confusion caused by type juggling.

Difference Between Type Casting and Type Juggling

FeatureType CastingType Juggling
Conversion TypeManualAutomatic
Who Controls ItDeveloperPHP Engine
Syntax(type)$variableImplicit (no syntax)
Use CaseWhen precision and control are requiredWhen flexibility and simplicity are fine

Practice Task

Create a PHP script to demonstrate:

  1. Type casting a string to an integer.

  2. Type juggling when adding a string and a number.

  3. The difference between == and === comparisons.

Hint Code:

<?php
$num1 = "20";
$num2 = 5;

$casted = (int)$num1;
$juggled = $num1 + $num2;

echo "Casted Value: $casted\n";
echo "Juggled Value: $juggled\n";
echo ("20" == 20) ? "Equal" : "Not Equal";
echo ("\n");
echo ("20" === 20) ? "Equal" : "Not Equal";
?>