Why does PHP convert String to Integer?

后端 未结 4 674
庸人自扰
庸人自扰 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 07:52

    From the manual

    When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

    If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

    The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

    • http://www.php.net/manual/en/language.types.type-juggling.php
    • http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-21 08:03

    From the PHP Manual:

    a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

    See Type Juggling and Type Comparison Table and String conversion to numbers for details:

    If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

    The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

    And the chapter on Strings:

    An integer or float is converted to a string representing the number textually (including the exponent part for floats). Floating point numbers can be converted using exponential notation (4.1E+6).

    And also (was already given by deceze though) the chapter on Comparison Operators:

    If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

    0 讨论(0)
  • 2020-12-21 08:10

    Because thats the way they designed the language, as described here.

    0 讨论(0)
提交回复
热议问题