note 36738 added to function.is-numeric

From: Date: Tue, 21 Oct 2003 00:05:23 +0000
Subject: note 36738 added to function.is-numeric
Groups: php.notes 
Request: Send a blank email to php-notes+get-58957@lists.php.net to get a copy of this message
the best way to check whether variable is numeric is to check its ascii code :) function is_num($var) { for ($i=0;$i<strlen($var);$i++) { $ascii_code=ord($var[$i]); if ($ascii_code >=49 && $asci_code <=57) continue; else return false; } return true; } this function can be usefull if you wont to chec eg $_POST function test_post($tab) { $post = array(); $post=$tab; echo $tab["user_name"]; foreach ($post as $varname => $varvalue) { if (empty($varvalue)) { $post[$varname] = null; }elseif (is_num($varvalue)) { $post[$varname]=trim($varvalue); $post[$varname]=strip_tags($varvalue); $post[$varname]=intval($varvalue); $post[$varname]=addslashes($varvalue); }else { $post[$varname]=NULL; } } }//forech return $post; } ---- Manual Page -- http://www.php.net/manual/en/function.is-numeric.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36738 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36738&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36738&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#58957) next »