note 36738 deleted from function.is-numeric by cmb

From: Date: Sun, 23 Jun 2019 10:31:57 +0000
Subject: note 36738 deleted from function.is-numeric by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211344@lists.php.net to get a copy of this message
Note Submitter: blazatek at wp dot pl ---- 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; }

« previous php.notes (#211344) next »