Why does PHP convert String to Integer?

后端 未结 4 678
庸人自扰
庸人自扰 2020-12-21 07:36

I would like to know on what condition PHP decides when to convert from String to Integer and the other way around

Example:

$key = 0;
$test = \'test         


        
4条回答
  •  眼角桃花
    2020-12-21 08:02

    It converts strings to numbers when either operand is a number. The manual puts it cryptically like this:

    http://php.net/manual/en/language.operators.comparison.php

    For various types, comparison is done according to the following table (in order).

    Operand 1           Operand 2
    
    null or string      string              Convert NULL to "", numerical
                                            or lexical comparison
    ...
    
    string, resource    string, resource    Translate strings and resources
    or number           or number           to numbers, usual math
    

    Note that the "in order" part is important when reading this table.

提交回复
热议问题