How to convert php character type to integer type: 1. Create a PHP sample file; 2. Pass "foreach($arr as $key=>$val){print($key == "aa" ? 5: $val);}" or "((int)('132very'));" to convert the character type into an integer.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
php character type to integer type
$arr = array(0=>1,"aa"=>2, 3, 4); foreach($arr as $key=>$val){ print($key == "aa" ? 5 : $val); }
Output result: 5534
why:
When two values are logically judged in PHP, if two If the types of the values are inconsistent, PHP will automatically convert the value on the right to the type on the left,
and then make a judgment. Therefore, the "aa" conversion integer is equal to 0, which is naturally equal to the 0 on the left.
Character type conversion to integer type
Example:
((int)('b')); //0 ((int)('very good')); //0 ((int)('132very')); //132 ((int)('ver1323')); //0
We can draw the rule: if it starts with a valid number, take the valid number. Anything starting with a non-valid number is converted to 0;
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert php character type to integer type. For more information, please follow other related articles on the PHP Chinese website!