so.. is this possible? because I see some native php functions can do that
for example strpos() can return 0 which can apparently be true
so.. is this possible? because I see some native php functions can do that
for example strpos() can return 0 which can apparently be true
When the manual says some function can return both integer 0 and boolean false, it means it can return either integer 0
or boolean false
(not both) in any given call. PHP is not strictly typed, functions can return different types in different situations. For instance, the following function returns either 0
or false
, depending on whether the passed parameter is non negative or not:
function myfunc($arg) {
if ($arg >= 0)
return 0;
else
return false;
}
PHP has no multiple return. You have two options:
function myfunc() {
return array(0, true); //return array
}
class MyOutputHolder {
private $number;
private $truth;
function getNumber { return $this->number; }
function getTruth { return $this->truth; }
function __construct($number, $truth) {
$this->number = $number;
$this->truth = $truth;
}
}
function myfunc() {
return new MyOutputHolder(0, true); //return object
}
A third possibility is a custom resource, but that must be implemented internally (in an extension).
function myfunc(&$outnumber, &$outtruth) {
$outnumber = 0;
$outtruth = true;
}
Of course, you can return only 0
or true
and use only one parameter.
A function can only ever have one return value. However, you can return an array with multiple values if you need to.
Does this count?
function stupid() {
return "0\0";
};
echo stupid() ."\n";
var_dump(stupid());
if (stupid()) echo "true\n";
echo stupid() + 4 . "\n";
Output:
0
string(2) "0"
true
4
*ducks*
For functions that can return successfully with the return value of zero, you should be using type equivalence checking.
if(somefunction() !== false) {
}
The integer zero is interpreted as false if type is not considered. For example, assuming somefunction returns zero.
somefunction() != false
Will be false, while
somefunction() !== false
Will be true.
Your confusion is that strpos returns the zero-based index of the search string.
So in this case, 0 is a valid return, it means "found it at index 0"
If the string isn't found at all, then it returns FALSE.
It's important to note what's written in big red warning in the strpos doc page:
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
ie: 0 is not exactly FALSE in php. It's fundamental of php. 0 == FALSE but 0 !== FALSE
As too why PHP can return either a numeric value or a boolean - maybe that's your actual question - PHP isn't strongly typed, you never specify what you'll be returning, so you're free to return different data types depending on the outcome of the function
I think you're misunderstanding what strpos actually returns...
strpos() returns either an integer greater than or equal to zero, or it returns false (if the needle character is not found in the string).
0 does not equal true in any sense - what the PHP documentation does mention, though, is that because of PHP's loose-typing, 0 can equal false unless you use the conditional operator that forces type as well as value comparison.
var_dump(0 == false); // 'true'
var_dump(0 == true); // 'false'
var_dump(0 === false); // 'false'
var_dump(0 === true); // 'false'
var_dump(0 !== false); // 'true'
this is why the PHP manual recommends you test the return value from strpos() with '!== false' because the character you are searching for may be the first character in the string and therefore the function returns 0.
$string = "_testing";
var_dump(strpos($string, '_')); // 0
var_dump(strpos($string, '_') !== false); // 'true'
var_dump(strpos($string, '_') === true); // 'false'