note 92611 deleted from function.return by googleguy
| From: | googleguy@php.net | Date: | Thu, 02 Jan 2014 23:16:05 +0000 |
| Subject: | note 92611 deleted from function.return by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198087@lists.php.net to get a copy of this message | ||
Note Submitter: fyrye
----
A side note when you return a conditional value the variable type will inherit its type of Boolean
For example
<?php
function foo($SQL){
$conTemp = new mysqli("locahost", "root", "", "");
$conTemp->select_db("MyDB");
return $conTemp->query($SQL) or die("Query Failed!");
}
$result = foo("SELECT UserName FROM Users LIMIT 1");
echo gettype($result); //returns Boolean instead of object or die
?>
Instead be explicit with your function like so
<?php
function foo($SQL){
$conTemp = new mysqli("locahost", "root", "", "");
$conTemp->select_db("MyDB");
if(!$result = $conTemp->query($SQL)){
return die("Query Failed");
}
return $result;
}
$result = foo("SELECT UserName FROM Users LIMIT 1");
echo gettype($result); //Now will return Object or die
?>