(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
(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
sample_cast.php
<?php
// Convert a string value to an integer
$power_str = "4200";
$power = (int) $power_str;
echo $power; // 4200
var_dump($power); // int(4200)
// Casting a decimal to an integer truncates the fractional part
echo (int) 3.9; // 3 (not rounded up)
// Strings that cannot be interpreted as a number become 0
echo (int) "abc"; // 0
echo (int) "123abc"; // leading numeric part converted: 123
// Casting to a string
echo (string) 100; // 100
echo (string) true; // 1
echo (string) false; // empty string
echo (string) null; // empty string
// Casting to a boolean
var_dump((bool) 1); // bool(true)
var_dump((bool) 0); // bool(false)
var_dump((bool) ""); // bool(false)
var_dump((bool) "0"); // bool(false) — note: "0" is false
var_dump((bool) "abc"); // bool(true)
// Casting to an array
$input = "sample_name";
print_r((array) $input); // becomes a single-element array
// Use settype() to change the type of the variable itself
$level = "8";
settype($level, "int");
var_dump($level); // int(8)
// intval() also supports base conversion via the second argument
echo intval("0xFF", 16); // interpreted as hexadecimal: 255
echo intval("077", 8); // interpreted as octal: 63
cast.php
php cast.php
4200
int(4200)
3
0
123
100
1
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
Array
(
[0] => sample_name
)
int(8)
255
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 explicit casting with (int) or (float) is common practice 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.
(int) cast truncates (does not round up)
(int) 3.9 evaluates to 3, not 4. Use ceil() to round up, or round() to round to the nearest integer.
<?php // Damage calculation (result has a fractional part) $base_damage = 380; $multiplier = 1.35; $raw = $base_damage * $multiplier; echo (int) $raw; // 513 (fractional part truncated) echo ceil($raw); // 514 (rounded up) echo round($raw); // 513 (rounded to nearest)
cast_truncate.php
php cast_truncate.php 513 514 513
Casting "abc" to (int) results in 0
Casting a string that cannot be interpreted as a number to an integer produces 0. PHP raises no warning. Using is_numeric() before casting is the safer approach.
Problematic pattern (direct cast silently produces 0):
<?php $input = "abc"; // example of form input $level = (int) $input; echo $level; // 0 (no warning)
cast_ng.php
php cast_ng.php 0
Check with is_numeric() before casting:
<?php
$input = "abc"; // example of form input
if (is_numeric($input)) {
$level = (int) $input;
} else {
$level = 1; // default value
echo "Invalid input. Using default: " . $level;
}
cast_ok.php
php cast_ok.php Invalid input. Using default: 1
Values that become false when cast to (bool)
0, "", "0", null, [], and 0.0 all become false. The string "0" becoming false is a particularly easy point to overlook.
<?php
// Values that become false
var_dump((bool) 0); // bool(false)
var_dump((bool) ""); // bool(false)
var_dump((bool) "0"); // bool(false) — the string "0" is also false
var_dump((bool) null); // bool(false)
var_dump((bool) []); // bool(false)
var_dump((bool) 0.0); // bool(false)
// Values that become true
var_dump((bool) "false"); // bool(true) — the string "false" is true
var_dump((bool) -1); // bool(true) — negative numbers are true
// Example: 0 treated as false
$rank = 0; // top rank
if ($rank) {
echo "Has rank";
} else {
echo "No rank (0 evaluates to false)";
}
cast_bool.php
php cast_bool.php bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(true) bool(true) No rank (0 evaluates to false)
When 0 is a valid value, use === null or isset() for the check instead.
Practical Patterns
Safe casting of form input
Receiving form values and safely casting them to integers.
<?php
// All values from $_POST are strings
$post_data = [
'username' => 'user1',
'power' => '9500',
'level' => '8',
'notes' => '',
];
$username = (string) $post_data['username'];
$power = is_numeric($post_data['power']) ? (int) $post_data['power'] : 0;
$level = is_numeric($post_data['level']) ? (int) $post_data['level'] : 1;
echo $username . ': Power ' . $power . ', Level ' . $level;
cast_form.php
php cast_form.php user1: Power 9500, Level 8
Damage calculation using cast
Casting float calculation results to integers to produce integer damage values.
<?php
$characters = [
['name' => 'unit_a', 'base' => 380, 'rate' => 1.35],
['name' => 'unit_b', 'base' => 360, 'rate' => 1.40],
['name' => 'unit_c', 'base' => 340, 'rate' => 1.30],
];
foreach ($characters as $f) {
$damage = (int) ($f['base'] * $f['rate']); // truncate float to integer
echo $f['name'] . ': ' . $damage . ' damage' . "\n";
}
cast_damage.php
php cast_damage.php unit_a: 513 damage unit_b: 504 damage unit_c: 442 damage
If you find any errors or copyright issues, please contact us.