$val){print($key == "aa" ? 5 : $val); }" method or "((int)('132very'));" method can be used to convert the character type into an integer type."/> $val){print($key == "aa" ? 5 : $val); }" method or "((int)('132very'));" method can be used to convert the character type into an integer type.">

Home >Backend Development >PHP Problem >How to convert php character type to integer type

How to convert php character type to integer type

藏色散人
藏色散人Original
2022-10-28 09:35:301752browse

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.

How to convert php character type to integer type

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is php only 64-bit?Next article:Is php only 64-bit?