note 76094 deleted from function.is-numeric by cmb

From: Date: Sun, 23 Jun 2019 10:37:16 +0000
Subject: note 76094 deleted from function.is-numeric by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211354@lists.php.net to get a copy of this message
Note Submitter: kendsnyder at gmail dot com ---- Be careful when using is_numeric() to escape SQL strings. is_numeric('0123') returns true but 0123 without quotes cannot be inserted into SQL. PHP interprets 0123 without quotes as a literal octal number; but SQL just throws a syntax error. <?php is_numeric('0123'); // true is_numeric(0.123); // true is_numeric('0.123'); // true is_numeric(123); // true is_numeric('123'); // true is_numeric('foo'); // false ?> Casting a value to float then string and comparing it to the original value cast as string as the same effect as is_numeric but returns false for numeric strings that begin with zero and have no decimal point. Examples: <?php function isDecimalNumber($n) { return (string)(float)$n === (string)$n; } isDecimalNumber('0123'); // false isDecimalNumber(0.123); // true isDecimalNumber('0.123'); // true isDecimalNumber(123); // true isDecimalNumber('123'); // true isDecimalNumber('foo'); // true ?>

« previous php.notes (#211354) next »