0Pricing
PHP Academy · Lesson

Type Juggling and Type Casting

Understand how PHP automatically converts between types and how to cast manually.

What Is Type Juggling?

Type juggling means PHP automatically converts a value's type when an operator or function requires a different type.

This is convenient but can cause subtle bugs if you don't understand the rules.

Juggling in Arithmetic

PHP converts strings to numbers when used in arithmetic context:

<?php
echo '5' + 3;       // 8  — string '5' becomes int 5
echo '5.5' + 1.5;   // 7  — string '5.5' becomes float
echo '5 cats' + 1;  // 6  — leading numeric part used, rest ignored
echo 'cats' + 1;    // 1  — non-numeric string becomes 0

All lessons in this course

  1. Declaring Variables in PHP
  2. PHP Data Types Overview
  3. Echo and Print Output
  4. Type Juggling and Type Casting
← Back to PHP Academy