search
Homephp教程php手册Notes on PHP weak type safety issues

1. Type conversion problem

 <span style="color: #008080;">intval</span><span style="color: #000000;">();

 

    </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">intval</span>('1asdfasd'));  <span style="color: #008000;">//</span><span style="color: #008000;">1</span>

    <span style="color: #008080;">var_dump</span>(<span style="color: #008080;">intval</span>('awqw12'));  <span style="color: #008000;">//</span><span style="color: #008000;">0</span>

    <span style="color: #008080;">var_dump</span>(<span style="color: #008080;">intval</span>(<span style="color: #0000ff;">array</span>()));    <span style="color: #008000;">//</span><span style="color: #008000;">0</span>

    <span style="color: #008080;">var_dump</span>(<span style="color: #008080;">intval</span>(<span style="color: #0000ff;">array</span>('foo','val'))); <span style="color: #008000;">//</span><span style="color: #008000;">1</span>

    <span style="color: #008080;">var_dump</span>(<span style="color: #008080;">intval</span>(0x1A)); <span style="color: #008000;">//</span><span style="color: #008000;">26  十六进制转换</span>

    <span style="color: #008080;">var_dump</span>(<span style="color: #008080;">intval</span>('asdfqwer')); <span style="color: #008000;">//</span><span style="color: #008000;">0</span>

   

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

Note: PHP uses 32-bit memory to store an integer. 32-bit can represent 4294967296 numbers. If there is a sign, 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 result will enter switch case 3. Why is this happening? If the switch is a numeric type case, the switch will convert the parameters into the int class. Therefore, when the above is executed, $i will be processed first. Type conversion, the conversion result is 3, so. . .

  <span style="color: #008080;">in_array</span><span style="color: #000000;">();

    </span><span style="color: #800080;">$arr</span> = [0,1,2,3,'test'<span style="color: #000000;">];

    </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">in_array</span>('abd',<span style="color: #800080;">$arr</span>));    <span style="color: #008000;">//</span><span style="color: #008000;"> true</span>

    <span style="color: #008080;">var_dump</span>(<span style="color: #008080;">in_array</span>('1bc',<span style="color: #800080;">$arr</span>));    <span style="color: #008000;">//</span><span style="color: #008000;"> true</span>

Why is the above execution result like this? After 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 As a 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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment