RE: [PHP] is_null() and is_string() reversed when using in switch case statements... [SOLVED]
| From: | Daevid Vincent | Date: | Fri, 15 Jul 2011 02:44:18 +0000 |
| Subject: | RE: [PHP] is_null() and is_string() reversed when using in switch case statements... [SOLVED] | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-314088@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Simon J Welsh [mailto:simon@welsh.co.nz]
> Sent: Thursday, July 14, 2011 7:29 PM
> To: Daevid Vincent
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] is_null() and is_string() reversed when using in switch
> case statements...
>
> On 15/07/2011, at 1:58 PM, Daevid Vincent wrote:
>
> > function test($v)
> > {
> > var_dump($v);
> >
> > if (is_string($v)) echo "FOUND A STRING.\n";
> > if (is_null($v)) echo "FOUND A NULL.\n";
> >
> > switch ($v)
> > {
> > case is_string($v):
> > echo "I think v is a string, so this is broken.\n";
> > break;
> > case is_null($v):
> > echo "I think v is null, so this is broken.\n";
> > break;
> > case is_object($v):
> > $v = '{CLASS::'.get_class($v).'}';
> > echo "I think v is a class $v\n";
> > break;
> >
> > case is_bool($v):
> > $v = ($v) ? '{TRUE}' : '{FALSE}';
> > echo "I think v is boolean $v\n";
> > break;
> > case is_array($v):
> > $v = '['.implode(',',$v).']';
> > echo "I think v is an array $v\n";
> > break;
> > case is_numeric($v):
> > echo "I think v is a number $v\n";
> > break;
> > }
> > }
>
> In both cases, $v is equivalent to false, so is_string(NULL) = false ==
NULL
> == $v, likewise for is_null($v);
>
> You're most likely after switch(true) { . } rather than switch($v)
> ---
> Simon Welsh
> Admin of http://simon.geek.nz/
Ah! Thanks Simon! That is exactly right. Doh! I should have thought of
that... *smacks head*
Here is the fruit of my labor (and your fix)...
/**
* Useful for debugging functions to see parameter names, etc.
* Based upon http://www.php.net/manual/en/function.func-get-args.php#103296
*
* function anyfunc($arg1, $arg2, $arg3)
* {
* debug_func(__FUNCTION__, '$arg1, $arg2, $arg3', func_get_args());
* //do useful non-debugging stuff
* }
*
* @access public
* @return string
* @param string $function_name __FUNCTION__ of the root function as
passed into this function
* @param string $arg_names the ',,,' encapsulated parameter list of
the root function
* @param array $arg_vals the result of func_get_args() from the root
function passed into this function
* @param boolean $show_empty_params (FALSE)
* @author Daevid Vincent
* @date 2011-17-14
* @see func_get_args(), func_get_arg(), func_num_args()
*/
function debug_func($function_name, $arg_names, $arg_vals,
$show_empty_params=FALSE)
{
$params = array();
echo $function_name.'(';
$arg_names_array = explode(',', $arg_names);
//var_dump($arg_names, $arg_names_array, $arg_vals );
foreach($arg_names_array as $k => $parameter_name)
{
$v = $arg_vals[$k];
//echo "k = $parameter_name = $k and v = arg_vals[k] = $v<br>\n";
switch(true)
{
case is_string($v): if ($show_empty_params) $v = "'".$v."'"
; break;
case is_null($v): if ($show_empty_params) $v = '{NULL}';
break;
case is_bool($v): $v = ($v) ? '{TRUE}' : '{FALSE}'; break;
case is_array($v): (count($v) < 10) ? $v =
'['.implode(',',$v).']' : $v = '[ARRAY]'; break;
case is_object($v): $v = '{CLASS::'.get_class($v).'}';
break;
case is_numeric($v): break;
}
if ($show_empty_params || $v) $params[$k] = trim($parameter_name).':
'.$v;
}
echo implode(', ',$params).")<br/>\n";
}