How to convert php character type to integer type

藏色散人
Release: 2023-03-17 10:10:01
Original
1701 people have browsed it

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);
 }
Copy after login

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
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!