search
Home类库下载PHP类库PHP weak type safety issues
PHP weak type safety issuesOct 09, 2016 pm 01:29 PM

Notes on PHP weak type safety issues

1. Type conversion issues

intval();

 

    var_dump(intval('1asdfasd'));  //1

    var_dump(intval('awqw12'));  //0

    var_dump(intval(array()));    //0

    var_dump(intval(array('foo','val'))); //1

    var_dump(intval(0x1A)); //26  十六进制转换

    var_dump(intval('asdfqwer')); //0

intval If the value is converted to a string, an error will not be returned, but 0 will be returned. If the value is converted to an array, there are two situations. Convert the value When it is an empty array, it will return 0, otherwise it will return 1

Note: PHP uses 32-bit memory to save an integer. 32-bit can represent 4294967296 numbers. If it is signed, it is -2147483647 to 2147483648;

2. The problem of looseness of built-in functions

switch();
 
 
 
    $i='3adcd';
 
    switch($i){
 
             case 1:
 
                    echo 'i is 1';
 
                    break;
 
             case 2:
 
                    echo 'i is 2';
 
                    break;
 
             case 3:
 
                    echo 'i is 3';
 
                    break;
 
             default:
 
                   echo 'i is default';
 
                   break;
 
    }

The above results will enter switch case 3. Why is this? If switch is a numeric type case judgment, switch will convert the parameters into int class, so the above execution When, $i is first type-converted, and the conversion result is 3, so. . .

in_array();

    $arr = [0,1,2,3,'test'];

    var_dump(in_array('abd',$arr));    // true

    var_dump(in_array('1bc',$arr));    // true

Why is the above execution result like this? Later, through querying the manual, the official statement is that in_array defaults to a loose comparison method, which only compares whether the values ​​are equal, but does not compare whether the types of the values ​​are the same, so the above For this result, you can set the third parameter of the in_array function. Setting it to True is a strict comparison method.

The above are the things we need to pay attention to in our daily development.


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft