Numeric stringsA PHP string is considered numeric if it can be interpreted as an int or a float. 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 of leading numeric strings. This is simply a string which starts like a numeric string followed by any characters.
Strings used in numeric contextsWhen a string needs to be evaluated as number (e.g. arithmetic operations, int type declaration, etc.) the following steps are taken to determine the outcome:
Behavior prior to PHP 8.0.0Prior to PHP 8.0.0, a string was considered numeric only if it had leading whitespaces, if it had trailing 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:
E_NOTICE
nor E_WARNING was raised.
|