note 98996 added to function.is-string

From: Date: Wed, 21 Jul 2010 09:40:20 +0000
Subject: note 98996 added to function.is-string
Groups: php.notes 
Request: Send a blank email to php-notes+get-171007@lists.php.net to get a copy of this message
Dear son9ne dot junk at gmail dot com. The thing is that it is your function fetchType is working wrong. It also will say that the following variables are strings: false, 0, null - though they are bool, integer and null respectfully. The mistake is that you compares $var with the result of is_<type>($var). If $var is some type then is_<type>($var) will return true and thereafter you should make the following code to receive correct answer: <?php function fetchType($var) { # Setup commonly used types (PHP.net warns against using gettype()) switch (true) { case is_string($var): $type='string'; break; case is_array($var): $type='array'; break; case is_null($var): $type='NULL'; break; case is_bool($var): $type='boolean'; break; case is_int($var): $type='integer'; break; case is_float($var): // $type='float'; $type='double'; break; case is_object($var): $type='object'; break; case is_resource($var): $type='resource'; break; default: $type='unknown type'; break; } return $type; } echo fetchType(''); ?> ---- Server IP: 195.54.192.44 Probable Submitter: 127.0.0.1 ---- Manual Page -- http://www.php.net/manual/en/function.is-string.php Edit -- https://master.php.net/note/edit/98996 Del: integrated -- https://master.php.net/note/delete/98996/integrated Del: useless -- https://master.php.net/note/delete/98996/useless Del: bad code -- https://master.php.net/note/delete/98996/bad+code Del: spam -- https://master.php.net/note/delete/98996/spam Del: non-english -- https://master.php.net/note/delete/98996/non-english Del: in docs -- https://master.php.net/note/delete/98996/in+docs Del: other reasons-- https://master.php.net/note/delete/98996 Reject -- https://master.php.net/note/reject/98996 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#171007) next »