← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 12 of 13

PHP Type Casting

Type casting is like pouring juice from a bottle into a cup: the drink stays the same but it now has a different shape. You tell PHP to treat a value, like the text five, as a different kind, like the number five.
Syntax
php
$result = (int) $value;
$result = (float) $value;
$result = (string) $value;
$result = (bool) $value;
$result = (array) $value;

What is Type Casting?

Type casting is explicitly telling PHP to treat a value as a different type by writing the target type in parentheses before it, like (int) $value.

It's a deliberate, visible conversion — unlike PHP's automatic type juggling, which happens silently whenever an operator needs operands of matching types.

Example: What is Type Casting?

php
<?php
// Declare `$value`, set to "42"
$value = "42";
// Declare `$intValue`, set to `(int) $value`
$intValue = (int) $value;
// Print a detailed dump (with types) of `$intValue`
var_dump($intValue);
?>

Casting to Integer and Float

(int) truncates toward zero rather than rounding — (int) 9.9 gives 9, not 10 — and stops reading a string at the first non-numeric character, so (int) "42abc" is 42.

(float) follows the same leading-numeric-prefix rule but preserves the decimal portion.

Example: Casting to Integer and Float

php
<?php
// Print `(int) 9.9 . "\n"` to the output
echo (int) 9.9 . "\n";
// Print `(int) "42abc" . "\n"` to the output
echo (int) "42abc" . "\n";
// Print `(float) "3.14xyz"` to the output
echo (float) "3.14xyz";
?>

Casting to String and Boolean

(string) converts numbers and booleans into their textual form (true becomes "1", false becomes ""), which matters because PHP does this automatically during concatenation anyway.

(bool) treats 0, 0.0, "", "0", empty arrays, and null as false — everything else, including the string "0.0", counts as true.

Example: Casting to String and Boolean

php
<?php
// Print a detailed dump (with types) of `(string) true`
var_dump((string) true);
// Print a detailed dump (with types) of `(string) false`
var_dump((string) false);
// Print a detailed dump (with types) of `(bool) "0.0"`
var_dump((bool) "0.0");
// Print a detailed dump (with types) of `(bool) ""`
var_dump((bool) "");
?>

Casting to Array and Object

(array) wraps a scalar in a single-element array, or converts an object's public properties into key-value pairs.

(object) does the reverse for an associative array, turning each key into a dynamic property — useful when an API expects stdClass objects instead of plain arrays.

Example: Casting to Array and Object

php
<?php
// Declare `$arr`, set to `(array) "hello"`
$arr = (array) "hello";
// Print a human-readable dump of `$arr`
print_r($arr);
// Declare `$obj`, set to `(object) ["name" => "Alice"]`
$obj = (object) ["name" => "Alice"];
// Print `$obj->name` to the output
echo $obj->name;
?>

Casting vs Automatic Type Juggling

PHP silently coerces types during comparisons and arithmetic ("5" + 3 becomes 8), which is convenient but can produce surprising results with loosely-typed data from user input.

Explicit casting makes the conversion visible in the code and is the safer choice whenever a value's type is uncertain.

Example: Casting vs Automatic Type Juggling

php
<?php
echo "5" + 3; // automatic juggling
echo "\n";
echo (int) "5" + 3; // explicit casting
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting (int) 9.9 to round to 10, when casting truncates toward zero and gives 9; use round() if rounding is needed.
  2. Casting user input with (int) "abc" and treating 0 as valid data, when non-numeric strings silently become 0.
  3. Assuming (bool) "0" is true because the string is non-empty, when "0" casts to false.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.