note 101140 deleted from function.gettype by thiago
| From: | thiago@php.net | Date: | Mon, 29 Nov 2010 13:25:19 +0000 |
| Subject: | note 101140 deleted from function.gettype by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174437@lists.php.net to get a copy of this message | ||
Note Submitter: PPKu-N0-SPAM-schy at mediasoft-berlin dot de
----
The code below can be optimized... why go through all the "if" conditions if there is
already found a match?
And why waste memory (even if this seems funny - think about running it several thousand times per
second with ... let's say 100 ... instances in parallel) by creating a variable to hold the
result?
Instead you should just return directly,
also this makes it is easier to read or add conditions.
Just place them before the final return statement and you're done.
<?php
/**
* Returns the type of the var passed.
*
* @param mixed $var Variable
* @return string Type of variable
*/
function myGetType($var)
{
if (is_array($var)) return "array";
if (is_bool($var)) return "boolean";
if (is_callable($var)) return "function reference";
if (is_float($var)) return "float";
if (is_int($var)) return "integer";
if (is_null($var)) return "NULL";
if (is_numeric($var)) return "numeric";
if (is_object($var)) return "object";
if (is_resource($var)) return "resource";
if (is_string($var)) return "string";
return "unknown type";
}
?>