(int) / (string) / (array) Cast Since: PHP 4(2000)
Converts the type of a variable to another type. PHP performs automatic type conversion, but explicit casting helps prevent unintended behavior.
Syntax
// Use a cast operator to convert the type. (int) $value; (string) $value; (float) $value; (bool) $value; (array) $value; // Use settype() to directly change the type of a variable. settype($var, $type);
Cast Types
| Cast | Description |
|---|---|
| (int) / (integer) | Converts to an integer. The decimal part is truncated. Strings that cannot be interpreted as a number become 0. |
| (string) | Converts to a string. Arrays become "Array", true becomes "1", and false and null become an empty string. |
| (float) / (double) | Converts to a floating-point number. Strings that cannot be interpreted as a number become 0.0. |
| (bool) / (boolean) | Converts to a boolean. 0, "", "0", null, and an empty array become false; everything else becomes true. |
| (array) | Converts to an array. A scalar value becomes a single-element array; an object has its properties converted to array keys. |
| settype($var, $type) | Changes the type of the variable itself. Specify "int", "string", "float", "bool", or "array" for $type. |
Sample Code
<?php
// Convert a string to an integer.
$str = "42";
$num = (int) $str;
echo $num; // Outputs "42".
var_dump($num); // Outputs "int(42)".
// Casting a decimal to an integer truncates the fractional part.
echo (int) 3.9; // Outputs "3".
// Strings that cannot be interpreted as a number become 0.
echo (int) "abc"; // Outputs "0".
echo (int) "123abc"; // The leading numeric part is converted; outputs "123".
// Casting to a string.
echo (string) 100; // Outputs "100".
echo (string) true; // Outputs "1".
echo (string) false; // Outputs an empty string.
echo (string) null; // Outputs an empty string.
// Casting to a boolean.
var_dump((bool) 1); // Outputs "bool(true)".
var_dump((bool) 0); // Outputs "bool(false)".
var_dump((bool) ""); // Outputs "bool(false)".
var_dump((bool) "0"); // Outputs "bool(false)".
var_dump((bool) "abc"); // Outputs "bool(true)".
// Casting to an array.
$val = "Hello";
print_r((array) $val); // Becomes a single-element array.
// Use settype() to change the type of the variable itself.
$price = "1980";
settype($price, "int");
var_dump($price); // Outputs "int(1980)".
// You can also convert using intval(). The second argument specifies the base.
echo intval("0xFF", 16); // Interpreted as hexadecimal; outputs "255".
echo intval("077", 8); // Interpreted as octal; outputs "63".
Notes
PHP type casting is a mechanism for explicitly converting a variable to a different type. Values received from forms are always strings, so it is recommended to explicitly cast them with (int) or (float) when using them in numeric calculations. PHP performs automatic type conversion, but relying on implicit conversion can lead to bugs.
A cast operator returns a new converted value and does not modify the original variable. Use settype() if you want to change the type of the variable itself.
Use is_array() / is_string() / is_numeric() to check the type of a variable. For checking whether a variable exists or is empty, isset() / empty() are useful.
If you find any errors or copyright issues, please contact us.