views:

153

answers:

7

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

+7  A: 

Edit

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;
}

Original

PHP has no multiple return. You have two options:

Return composite values instead

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).

Use output parameters

function myfunc(&$outnumber, &$outtruth) {
    $outnumber = 0;
    $outtruth = true;
}

Of course, you can return only 0 or true and use only one parameter.

Artefacto
You forgot one: Return an object instead.
Pekka
well for example this function:http://php.net/manual/en/function.strpos.php(strpos)can return both integer 0 and boolean false;how can i do that?
Alex
@Pek You're right; I've added it.
Artefacto
@Alex The page said `"Returns the position as an integer. If needle is not found, strpos() will return boolean FALSE."` It does not return `true`. It returns an integer `GEQ 0`, `false`, or `""`.
digitalFresh
well this is what I want :)i managed to come out with something like this:if($x = is_this_active() !== FALSE) { ...... echo $x; } the problem is that $x is 1, while is_this_active() is 0.is this a php bug?
Alex
@Alex you're missing parentheses. It should be `if(($x = is_this_active()) !== FALSE)`
Artefacto
$x = is_this_active();if($x !== FALSE) { echo $x;}
digitalFresh
@Artefacto thanks! that was it :)
Alex
ps: sorry about the confusing questions. I should have explained better that I wanted to know how to check if the return value is 0 or false
Alex
A: 

A function can only ever have one return value. However, you can return an array with multiple values if you need to.

Daniel Egeberg
+1  A: 

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*

MooGoo
A: 
return 0 && true;
digitalFresh
`return 0 and true;` is valid php too :)
svens
George Marian
Zurahn
George Marian
I guess this is more of a joke than a real answer. It returns zero and true, but not both zero and true.
svens
@svens That's the reason I reversed my down vote.
George Marian
Tough crowd in tonight.
Dont deny that *both* `0` and `true` are inside the `return` statement. My answer can only be as helpful as the question.
digitalFresh
I'd probably have gone with `return "00 ` :P
@MrXexxed $array = explode(" :P
Zurahn
+4  A: 

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.

Zurahn
+2  A: 

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

Ben
hah - posted my answer at the same time...
HorusKol
haha, actually I was 0.000005 seconds faster
Ben
A: 

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'
HorusKol