Numeric strings

A 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]*)[\.]) | ([\.][0-9]*)
EXPONENT_DNUM    (( | ) [eE][+-]? )
INT_NUM_STRING    [+-]?  
FLOAT_NUM_STRING  [+-]? ( | ) 
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.

Hinweis:

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

<?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 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:

  1. If the string is numeric, resolve to an int if the string is an integer numeric string and fits into the limits of the int type limits (as defined by PHP_INT_MAX), otherwise resolve to a float.
  2. If the context allows leading numeric strings and the string is one, resolve to an int if the leading part of the string is an integer numeric string and fits into the limits of the int type limits (as defined by PHP_INT_MAX), otherwise resolve to a float. Additionally an error of level E_WARNING is raised.
  3. The string is not numeric, throw a TypeError.

Behavior prior to PHP 8.0.0

Prior 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:

  • The usage of a leading numeric string would raise an E_NOTICE instead of an E_WARNING.
  • If the string is not numeric, an E_WARNING was raised and the value 0 would be returned.
Prior to PHP 7.1.0, neither E_NOTICE nor E_WARNING was raised.

<?php
$foo 
"10.5";                // $foo is float (11.5)
$foo "-1.3e3";              // $foo is float (-1299)
$foo "bob-1.3e3";           // TypeError as of PHP 8.0.0, $foo is integer (1) previously
$foo "bob3";                // TypeError as of PHP 8.0.0, $foo is integer (1) previously
$foo "10 Small Pigs";       // $foo is integer (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously
$foo "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
?>

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Restposten bei SMM.de

barneybrowning Würdest du bitte in diesem und auch in anderen Foren mit deiner Spamerei aufhören!!!

Geschrieben von scatello am 20.09.2024 18:16:31
Forum: News / Kostenloses
Restposten bei SMM.de

Danke für die Info zu den Restposten bei SMM! Es ist immer spannend, solche Angebote zu entdecken, vor allem wenn sie sich gut in die eigenen Pro ...

Geschrieben von barneybrowning am 19.09.2024 18:43:28
Forum: News / Kostenloses
Wie finde ich den Installationsordner einer App?

Vielen Dank für die hilfreiche Diskussion hier! Ich stand kürzlich vor dem gleichen Problem und wusste nicht, wie ich den Installationsordner ei ...

Geschrieben von barneybrowning am 19.09.2024 17:39:27
Forum: Windows (Client/Server)
suche tutorials für app entwicklung!

Ich bin auf der Suche nach Tutorials für die App-Entwicklung. Alexander schlägt vor, die Beiträge hier im Forum (https://alexanderriedinger.de) ...

Geschrieben von Leoniesteiner am 18.09.2024 21:46:07
Forum: Tutorials