Movatterモバイル変換


[0]ホーム

URL:


update page now
    Arrays »
    « Strings

    Numeric strings

    A PHPstring is considered numeric if it can be interpreted as anint or afloat.

    Formally as of PHP 8.0.0:

    WHITESPACES      \s*LNUM             [0-9]+DNUM             ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)EXPONENT_DNUM    (({LNUM} | {DNUM}) [eE][+-]? {LNUM})INT_NUM_STRING   {WHITESPACES} [+-]? {LNUM} {WHITESPACES}FLOAT_NUM_STRING {WHITESPACES} [+-]? ({DNUM} | {EXPONENT_DNUM}) {WHITESPACES}NUM_STRING       ({INT_NUM_STRING} | {FLOAT_NUM_STRING})

    PHP also has a concept ofleading numeric strings. This is simply a string which starts like a numeric string followed by any characters.

    Note:

    Any string that contains the letterE (case insensitive) bounded by numbers will be seen as a number expressed in scientific notation. This can produce unexpected results.

    Example #1 Scientific Notation Comparisons

    <?php
    var_dump
    ("0D1"=="000");// false, "0D1" is not scientific notation
    var_dump("0E1"=="000");// true, "0E1" is 0 * (10 ^ 1), or 0
    var_dump("2E1"=="020");// true, "2E1" is 2 * (10 ^ 1), or 20
    ?>

    Strings used in numeric contexts

    When astring needs to be evaluated as number (e.g. arithmetic operations,int type declaration, etc.) the following steps are taken to determine the outcome:

    1. If thestring is numeric, resolve to anint if thestring is an integer numeric string and fits into the limits of theint type limits (as defined byPHP_INT_MAX), otherwise resolve to afloat.
    2. If the context allows leading numeric strings and thestring is one, resolve to anint if the leading part of thestring is an integer numeric string and fits into the limits of theint type limits (as defined byPHP_INT_MAX), otherwise resolve to afloat. Additionally an error of levelE_WARNING is raised.
    3. Thestring is not numeric, throw aTypeError.

    Behavior prior to PHP 8.0.0

    Prior to PHP 8.0.0, astring was considered numeric only if it hadleading whitespaces, if it hadtrailing whitespaces then the string was considered to be leading numeric.

    Prior to PHP 8.0.0, when a string was used in a numeric context it would perform the same steps as above with the following differences:

    • The usage of a leading numeric string would raise anE_NOTICE instead of anE_WARNING.
    • If the string is not numeric, anE_WARNING was raised and the value0 would be returned.
    Prior to PHP 7.1.0, neitherE_NOTICE norE_WARNING was raised.

    <?php
    $foo
    =1+"10.5";// $foo is float (11.5)
    $foo=1+"-1.3e3";// $foo is float (-1299)
    $foo=1+"bob-1.3e3";// TypeError as of PHP 8.0.0, $foo is integer (1) previously
    $foo=1+"bob3";// TypeError as of PHP 8.0.0, $foo is integer (1) previously
    $foo=1+"10 Small Pigs";// $foo is integer (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously
    $foo=4+"10.2 Little Piggies";// $foo is float (14.2) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously
    $foo="10.0 pigs "+1;// $foo is float (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously
    $foo="10.0 pigs "+1.0;// $foo is float (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously
    ?>

    Found A Problem?

    Learn How To Improve This PageSubmit a Pull RequestReport a Bug
    add a note

    User Contributed Notes

    There are no user contributed notes for this page.
    To Top
    and to navigate •Enter to select •Esc to close •/ to open
    PressEnter without selection to search using Google

    [8]ページ先頭

    ©2009-2026 Movatter.jp