PHP Casting
Converting between data types
🔄 What is Type Casting?
Type casting converts a variable from one data type to another. PHP allows you to cast strings to numbers, numbers to strings, and between other types for flexible data manipulation.
<?php
$string = "123";
$number = (int)$string; // Cast to integer
echo "String: $string<br>";
echo "Number: $number";
?>
Output:
String: 123
Number: 123
Casting Types
To Integer
Convert values to whole numbers using (int) or (integer) cast. Strings with numbers become integers, removing decimals.
<?php
$str = "42";
$int = (int)$str;
echo $int; // 42
?>
To Float
Convert values to decimal numbers using (float) or (double) cast. Useful for precise calculations and monetary values.
<?php
$str = "3.14";
$float = (float)$str;
echo $float; // 3.14
?>
To String
Convert values to text using (string) cast. Numbers and booleans become their string representations for display.
<?php
$num = 123;
$str = (string)$num;
echo $str; // "123"
?>
To Boolean
Convert values to true/false using (bool) cast. Empty values become false, non-empty become true for logic.
<?php
$num = 1;
$bool = (bool)$num;
echo $bool; // true
?>
To Array
Convert values to arrays using (array) cast. Single values become one-element arrays for consistent processing.
<?php
$str = "hello";
$arr = (array)$str;
print_r($arr);
?>
To Object
Convert values to objects using (object) cast. Arrays become objects with properties matching array keys.
<?php
$arr = ["name" => "John"];
$obj = (object)$arr;
echo $obj->name;
?>
🔹 Casting to Integer
Convert different types to integers:
<?php
// String to integer
$str = "123";
$int = (int)$str;
echo "String to int: $int<br>";
// Float to integer (removes decimals)
$float = 3.99;
$int2 = (int)$float;
echo "Float to int: $int2<br>";
// Boolean to integer
$bool = true;
$int3 = (int)$bool;
echo "Boolean to int: $int3<br>";
// String with text
$mixed = "42 apples";
$int4 = (int)$mixed;
echo "Mixed to int: $int4";
?>
Output:
String to int: 123
Float to int: 3
Boolean to int: 1
Mixed to int: 42
🔹 Casting to Float
Convert different types to floats:
<?php
// String to float
$str = "3.14";
$float = (float)$str;
echo "String to float: $float<br>";
// Integer to float
$int = 42;
$float2 = (float)$int;
echo "Integer to float: $float2<br>";
// String with decimal
$price = "19.99";
$float3 = (float)$price;
echo "Price to float: $float3<br>";
// Using doubleval()
$str2 = "2.5";
$float4 = doubleval($str2);
echo "Using doubleval: $float4";
?>
Output:
String to float: 3.14
Integer to float: 42
Price to float: 19.99
Using doubleval: 2.5
🔹 Casting to String
Convert different types to strings:
<?php
// Integer to string
$num = 123;
$str = (string)$num;
echo "Integer to string: '$str'<br>";
// Float to string
$float = 3.14;
$str2 = (string)$float;
echo "Float to string: '$str2'<br>";
// Boolean to string
$bool = true;
$str3 = (string)$bool;
echo "Boolean to string: '$str3'<br>";
// Using strval()
$num2 = 456;
$str4 = strval($num2);
echo "Using strval: '$str4'";
?>
Output:
Integer to string: '123'
Float to string: '3.14'
Boolean to string: '1'
Using strval: '456'
🔹 Casting to Boolean
Convert different types to booleans:
<?php
// Integer to boolean
$num1 = 1;
$bool1 = (bool)$num1;
echo "1 to bool: " . ($bool1 ? "true" : "false") . "<br>";
$num2 = 0;
$bool2 = (bool)$num2;
echo "0 to bool: " . ($bool2 ? "true" : "false") . "<br>";
// String to boolean
$str1 = "hello";
$bool3 = (bool)$str1;
echo "'hello' to bool: " . ($bool3 ? "true" : "false") . "<br>";
$str2 = "";
$bool4 = (bool)$str2;
echo "Empty string to bool: " . ($bool4 ? "true" : "false");
?>
Output:
1 to bool: true
0 to bool: false
'hello' to bool: true
Empty string to bool: false
🔹 Casting to Array
Convert different types to arrays:
<?php
// String to array
$str = "hello";
$arr1 = (array)$str;
echo "String to array: ";
print_r($arr1);
echo "<br>";
// Integer to array
$num = 42;
$arr2 = (array)$num;
echo "Integer to array: ";
print_r($arr2);
echo "<br>";
// Object to array
$obj = (object)["name" => "John", "age" => 30];
$arr3 = (array)$obj;
echo "Object to array: ";
print_r($arr3);
?>
Output:
String to array: Array ( [0] => hello )
Integer to array: Array ( [0] => 42 )
Object to array: Array ( [name] => John [age] => 30 )
🔹 Automatic Type Conversion
PHP automatically converts types in some situations:
<?php
// String + Number
$result1 = "10" + 5;
echo "String + Number: $result1<br>";
// String concatenation
$result2 = "10" . 5;
echo "String concatenation: $result2<br>";
// Comparison
$str = "10";
$num = 10;
echo "String == Number: ";
echo ($str == $num) ? "true" : "false";
echo "<br>";
// Strict comparison
echo "String === Number: ";
echo ($str === $num) ? "true" : "false";
?>
Output:
String + Number: 15
String concatenation: 105
String == Number: true
String === Number: false