note 76094 added to function.is-numeric

From: Date: Fri, 29 Jun 2007 17:49:03 +0000
Subject: note 76094 added to function.is-numeric
Groups: php.notes 
Request: Send a blank email to php-notes+get-128093@lists.php.net to get a copy of this message
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 ?> ---- Server IP: 208.69.120.35 Probable Submitter: 216.194.126.99 ---- Manual Page -- http://www.php.net/manual/en/function.is-numeric.php Edit -- https://master.php.net/note/edit/76094 Del: integrated -- https://master.php.net/note/delete/76094/integrated Del: useless -- https://master.php.net/note/delete/76094/useless Del: bad code -- https://master.php.net/note/delete/76094/bad+code Del: spam -- https://master.php.net/note/delete/76094/spam Del: non-english -- https://master.php.net/note/delete/76094/non-english Del: in docs -- https://master.php.net/note/delete/76094/in+docs Del: other reasons-- https://master.php.net/note/delete/76094 Reject -- https://master.php.net/note/reject/76094 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128093) next »