note 101140 added to function.gettype
| From: | PPKu-N0-SPAM-schy at mediasoft-berlin dot de | Date: | Mon, 29 Nov 2010 13:07:21 +0000 |
| Subject: | note 101140 added to function.gettype | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174434@lists.php.net to get a copy of this message | ||
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";
}
?>
----
Server IP: 85.214.154.209
Probable Submitter: 93.219.148.178
----
Manual Page -- http://www.php.net/manual/en/function.gettype.php
Edit -- https://master.php.net/note/edit/101140
Del: integrated -- https://master.php.net/note/delete/101140/integrated
Del: useless -- https://master.php.net/note/delete/101140/useless
Del: bad code -- https://master.php.net/note/delete/101140/bad+code
Del: spam -- https://master.php.net/note/delete/101140/spam
Del: non-english -- https://master.php.net/note/delete/101140/non-english
Del: in docs -- https://master.php.net/note/delete/101140/in+docs
Del: other reasons-- https://master.php.net/note/delete/101140
Reject -- https://master.php.net/note/reject/101140
Search -- https://master.php.net/manage/user-notes.php