sunjie
sunjie

Reputation: 2053

Is it correct way to return value in PHP

I have very basic question regarding return value of a function, and checking the variable value.

function test($var1, $var2){
    if ($var1 == $var2){
            $var3 = "abc";
            return $var3;
        }
    return false
}

$value = test($var1, $var2);

if ($value){
    echo "Value is".$value;  //should output abc.
} else {
    echo "Not equal";
}
  1. Is it ok to either return a value or return false? For example I am not returning TRUE, it is ok?

  2. When i call the function, i store the return value in a variable $value. How can i check the function did return the $var3? Which of the if condition should be used? if (!empty($value)) or if (isset($value)) or if ($value) or if (value != false)

Upvotes: 1

Views: 2011

Answers (7)

Tomalak
Tomalak

Reputation: 338238

Yes, it is common practice in PHP to return FALSE as an indicator of an error condition. (What constitutes an error is your own decision and depends on what the function is supposed to do.)

However, since PHP automatically casts values to Boolean that are of another type (like the empty string or 0, which evaluate to FALSE as well), you should do an explicit check for FALSE like this:

if ($value !== FALSE) ...

As Felix Kling notes in the comments, this is called "strict comparison" (or "identity comparison"). It checks if a value is identical to FALSE, where as != FALSE, == FALSE and if ($value) only check if a value could be interpreted as FALSE.

Upvotes: 5

Peter Kruithof
Peter Kruithof

Reputation: 10764

The only right answer here is: it depends.

I always ask myself this question when creating a function like this. To answer it, I analyze what the function does, instead of what it returns.

For instance, if I have a getter, I expect to get a value, or nothing. In this case I often return null when nothing is found/something went wrong. A test function like yours should return a boolean at all times, in my opinion. Returning a variable when you're checking for something to be true or false is semantically incorrect, I think.

Aside from the semantics: returning 0, false or null does not really matter when you're checking it with if (test($var1, $var2)), since it will all work the same. However, if you want some finer details, you want to do an identity check (===) rather than a equality check. In PHP this is sometimes the case, for instance strpos can return 0 or false, 0 being a match is found, and false is not. Therefore the following would fail:

// returns 0, which is casted to false, so the 'else' part is executed
if (strpos('a', 'abc')) { 
    // 'abc' contains 'a'
} else {
    // 'abc' does not contain 'a'
}

So, long story short: it depends...

Upvotes: 0

Bing
Bing

Reputation: 746

  1. Yes, you can return pretty much anything from the function, or you can just "return" without returning anything. In your example, you'll get a string or "false" in return.

  2. To check for false you either do if (!$variable) or if ($variable===false). Zero will return true if you do "if ($variable==false)" due to auto casting of zero to false (and any other positive number to true). Three "===" makes sure it really is false and nothing else. The isset($var) checks for existance, not value - and is not applicable to your example since your function will return a value or "false" and thus always exists.

Upvotes: 0

deceze
deceze

Reputation: 522175

$var3 = "abc";
return $var3;

That's pointless. You're returning a value, not a variable. return "abc"; is perfectly fine.

Is it ok to either return a value or return false?

Yes, that's perfectly fine for a simple case such as this.

How can i check the function did return the $var3?

As said above, the function returns the value "abc", not $var3. You're saving it in a new variable $value. This variable is definitely set (you just created it right there), so there's no need for isset or empty. Just test whether its value is true or false (or whatever else you want to test for). So the way you're doing it in fine.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96266

  1. It's perfectly OK to return different data types.

  2. If you want to check against false, use: if ($value !== false). If you get lost which condition to use, this will clarify it: http://www.php.net/manual/en/types.comparisons.php

Upvotes: 1

Tudor Constantin
Tudor Constantin

Reputation: 26861

Your function returns false, so I would go with that check: if ($value != false)

Upvotes: 0

Jacob
Jacob

Reputation: 43229

I'm not a PHP developer, but I don't think your first approach works. There are other things than the boolean value false interpreted as false:

When converting to boolean, the following values are considered FALSE:

* the boolean FALSE itself
* the integer 0 (zero)
* the float 0.0 (zero)
* the empty string, and the string "0"
* an array with zero elements
* an object with zero member variables (PHP 4 only)
* the special type NULL (including unset variables)
* SimpleXML objects created from empty tags

http://php.net/manual/en/language.types.boolean.php

Upvotes: 1

Related Questions