Type casting in PHP is the explicit process of converting a variable’s value from one data type to another.
Since PHP is a dynamically and loosely (weakly) typed language, the interpreter automatically determines the type of a variable at runtime and can convert values between compatible types when needed. This is called implicit type conversion in PHP, which automatically performs implicit type conversion.
However, in some situations, you need to explicitly convert a variable to a specific type — this is called type casting in PHP. This feature gives developers direct control over the data type of a variable.
Basic Syntax of Type Casting in PHP
In PHP, we explicitly cast a variable by placing the desired data type inside parentheses before the variable or value we want to convert. This tells PHP to convert the value into that specific type. The general syntax to perform type casting in PHP is:
(new_type) $variable;
Example 1:
<?php
$var = "100";
$intVar = (int) $var;
echo $intVar;
?>Output:
100
In this example:
- We have defined a variable named $var and assigned the string value 100 to it.
- Then, we have performed explicit type casting.
- In the line $intVar = (int) $var;, the (int) is the type casting operator that tells PHP to convert the value of $var into an integer. Therefore, the string “100” is converted to the integer 100.
- After that, we have stored the converted value in a new variable named $intVar.
- The line echo $intVar; prints the value of $intVar.
Types of Type Casting in PHP
PHP supports the following types of type casting:
- (int) or (integer) → Convert to data type integer
- (float) or (double) or (real) → Convert to data type float
- (string) → Convert to data type string
- (bool) or (boolean) → Convert to data type boolean
- (array) → Convert to data type array
- (object) → Convert to data type object
- (unset) → Convert to data type NULL (deprecated/not commonly used)
Let us understand each type casting with the help of examples.
Integer Casting in PHP
Integer casting in PHP is the process of converting a variable’s value into an integer (whole number) using type casting operator (int) or (integer). It is a simple but powerful feature that allows you to convert values into whole numbers, perform accurate calculations, and avoid unexpected type-related errors.
Let us take an example based on integer casting with different data types.
Example 2:
<?php
$a = 10; // Integer
$b = 10.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (int) $a;
$b = (int) $b;
$c = (int) $c;
$d = (int) $d;
$e = (int) $e;
// Use var_dump() to verify the data type and value.
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>Output:
int(10) int(10) int(0) int(1) int(0)
Rules of Integer Casting in PHP
There are the following rules of integer casting in PHP that you should keep in mind. They are:
- Integer → Integer
When you cast an integer to an integer, there is no change in value. - Float → Integer
The decimal part is removed (truncated).
Example: 5.9 → 5 - No Rounding
Integer casting does not round values.
Example: (int)10.99 → 10 - Numeric String → Integer
Only the starting numeric part of the string is considered.
Example: “50abc” → 50 - Non-numeric String → Integer
If a string starts with non-numeric characters, the result is 0.
Example: “abc50” → 0 - Boolean → Integer
true → 1
false → 0 - NULL → Integer
NULL is converted to 0
Example 3:
<?php
$a = (int)10.99;
$b = (int)"50abc";
$c = (int)"abc50";
// Display results.
echo "Casting (int)10.99: ";
var_dump($a);
echo "<br>";
echo "Casting (int)\"50abc\": ";
var_dump($b);
echo "<br>";
echo "Casting (int)\"abc50\": ";
var_dump($c);
?>Output:
Casting (int)10.99: int(10) Casting (int)"50abc": int(50) Casting (int)"abc50": int(0)
String Casting in PHP
String casting in PHP is the process of converting a variable’s value into a string using type casting operator (string). It converts any type of data into a readable text format. String casting is especially useful when you want to display values in string form or concatenate different data types into a string.
Example 4:
<?php
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;
// Use var_dump() to verify type and value
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
?>Output:
string(1) "5" string(4) "5.34" string(5) "hello" string(1) "1" string(0) ""
Rules of String Casting in PHP
There are the following rules of string casting in PHP:
- When you cast an integer to a string, it is converted into its textual representation. Example: 5 → “5”
- When you cast a float to a string, the number is converted into a string including its decimal part. Example: 5.34 → “5.34”
- If you cast a string to a string, there is no change in value.
- If you cast a boolean data type to a string type:
- true is converted to “1”.
- false is converted to an empty string “”.
- When you cast NULL to a string, it is converted to an empty string “”
- String casting always produces a value of string data type, regardless of the original type.
Float Casting in PHP
Float casting in PHP is the process of converting a variable’s value into a floating-point number (decimal number) using (float) type casting operator. This type of casting allows you to convert different data types into decimal numbers for accurate calculations.
Example 5:
<?php
$a = 5; // Integer
$b = 5.75; // Float
$c = "123.45"; // Numeric string
$d = "hello"; // Non-numeric string
$e = true; // Boolean
$f = NULL; // NULL
$a = (float) $a;
$b = (float) $b;
$c = (float) $c;
$d = (float) $d;
$e = (float) $e;
$f = (float) $f;
// Check type and value
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
var_dump($f);
?>Output:
float(5) float(5.75) float(123.45) float(0) float(1) float(0)
Rules of Float Casting in PHP
There are the following rules of float casting in PHP:
- When you cast an integer to a float, it is converted into a decimal number. Example: 5 → 5.0
- When you cast a float to a float, there is no change in value.
- When you cast a numeric string to a float, it is converted into its decimal equivalent. Example: “123.45” → 123.45
- When you cast a string that starts with numeric values, only the starting numeric part is considered. Example: “50.5abc” → 50.5
- When you cast a non-numeric string to a float, the result is 0. Example: “hello” → 0
- When you cast a boolean to a float:
- true is converted to 1.0.
- false is converted to 0.0.
- When you cast NULL to a float, it is converted to 0.0.
- Float casting always results in a value of float (decimal) data type.
Boolean Casting in PHP
Boolean casting in PHP is the process of converting a variable’s value into a boolean (true or false) using a type casting operator (bool) or (boolean). This type of casting converts different types of values into true or false, which is essential for decision-making in programs.
Example 6:
<?php
$x = 10; // Integer
$y = 3.14; // Float
$z = 0; // Integer
$p = -5; // Integer
$q = 0.0; // Float
$r = "PHP"; // String
$s = ""; // Empty String
$t = false; // Boolean
$u = NULL; // NULL
$x = (bool) $x;
$y = (bool) $y;
$z = (bool) $z;
$p = (bool) $p;
$q = (bool) $q;
$r = (bool) $r;
$s = (bool) $s;
$t = (bool) $t;
$u = (bool) $u;
// Check results
var_dump($x);
var_dump($y);
var_dump($z);
var_dump($p);
var_dump($q);
var_dump($r);
var_dump($s);
var_dump($t);
var_dump($u);
?>Output:
bool(true) bool(true) bool(false) bool(true) bool(false) bool(true) bool(false) bool(false) bool(false)
Rules of Boolean Casting in PHP
Following are the rules of boolean casting in PHP:
- When you cast non-zero numbers into booleans, PHP converts them into true.
- When you cast zero (0) and 0.0 into boolean, PHP converts them into false.
- When you cast a non-empty string into a boolean type, PHP converts it into true.
- When you cast an empty string (“”) into a boolean type, PHP converts it into false.
- PHP treats the string “0” as a special case and converts it into false.
- PHP converts boolean true into true and boolean false into false (no change).
- PHP converts NULL and an empty array ([]) into false.
- PHP converts all other values into true.
Array Casting in PHP
Array casting in PHP is the process of converting a variable’s value of any data type into an array using a type casting operator (array).
Example 7:
<?php
$a = 10; // Integer
$b = 5.75; // Float
$c = "Hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (array) $a;
$b = (array) $b;
$c = (array) $c;
$d = (array) $d;
$e = (array) $e;
// Check results
print_r($a);
print_r($b);
print_r($c);
print_r($d);
print_r($e);
?>Output:
Array ( [0] => 10 ) Array ( [0] => 5.75 ) Array ( [0] => Hello ) Array ( [0] => 1 ) Array ( )
Rules of Array Casting in PHP
The rules of array casting in PHP are:
- PHP converts a scalar value (int, float, string) into an array with a single element at index 0.
- PHP converts a boolean value into an array:
- true becomes [1].
- false becomes [ ] (empty array).
- PHP converts a NULL value into an empty array.
- PHP keeps an array unchanged when you cast an array to an array.
- PHP converts an object into an array associative array where
- The property values become array elements.
- The property names become keys.
Object Casting in PHP
Object casting in PHP is the process of converting a variable’s value into an object using a type casting operator (object). This type of casting allows you to convert any data type into an object, making it easier to access data using properties. When you cast a value to an object, PHP creates an instance of the stdClass and assigns properties to it.
Example 8:
<?php
$a = 10; // Integer
$b = 5.75; // Float
$c = "Hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$f = ["x" => 1, "y" => 2]; // Array
$a = (object) $a;
$b = (object) $b;
$c = (object) $c;
$d = (object) $d;
$e = (object) $e;
$f = (object) $f;
// Check results
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
var_dump($f);
?>Output:
object(stdClass) { ["scalar"]=> int(10) }
object(stdClass) { ["scalar"]=> float(5.75) }
object(stdClass) { ["scalar"]=> string(5) "Hello" }
object(stdClass) { ["scalar"]=> int(1) }
object(stdClass) { }
object(stdClass) { ["x"]=> int(1), ["y"]=> int(2) }Rules of Object Casting in PHP
The rules of object casting in PHP are:
- PHP converts a scalar value (int, float, string, boolean) into an object with a property named scalar.
- PHP converts a NULL value into an empty object.
- PHP converts an indexed array into an object with the index number as property name and the value as property value.
- PHP converts an associative array into an object with the keys as property names and values as property values.
- PHP keeps an object unchanged when you cast an object to an object.





