Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP return false with a string

I am looking for the correct way to handle a return statement with a bool/string. For example I do all my checking inside the function and return true if it all passes. However if something went wrong I would like to return a string of what went wrong rather than just return false; with a general string. Does php assume false if a var is set to anything besides true? What is the correct way to handle this? Here's an example of what I'm doing

<?php
$a = 2;

$result = CheckVar($a);
if ($result)
{
    echo 'Correct!';
}
else
{
    echo $result;
}

function CheckVar($var)
{
    if ($var == 1)
    {
        return true;
    }
    else
    {
        return 'This is not the correct answer. You supplied '.$var;
    }
}
?>

It seems this method works, however is this good programming etiquette? Or is there another way I should be doing this? Thank you for your time.

like image 381
Ed R Avatar asked Oct 10 '11 04:10

Ed R


People also ask

What is return false in PHP?

return $oh || false does not work in PHP like it works in JavaScript. It would always return a boolean value (true or false).

What does ?: Mean in PHP?

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

Is 1 true or false PHP?

Value 0 and 1 is equal to false and true in php.

Is empty string false in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.


1 Answers

Does php assume false if a var is set to anything besides true?

Not at all. PHP will return whatever the variable was set to. And actually since you have a non-empty string, that's a "truthy" value (ie: true in a boolean context). Since you used if ($result) as your check and you return a "truthy" value, the condition is always true. You need to change that check to:

if ($result === true) {
    ...

What is the correct way to handle this?

I think it's a good enough way to handle it. An alternative would be to pass an error string variable by reference, and have the fail part of your code fill that, eg:

function check($var, &$error) {
    if ($var == 1) {
        return true;
    } else {
        $error = 'This is not the correct answer. You supplied ' . $var;
        return false;
    }
}

Some native PHP functions behave like this (eg: exec().) Yet another alternative is to return an array with the errors, like Jared suggested. I personally use this option when I expect multiple errors (eg: a form validation routine):

function check_stuff($stuff) {
    $errors = array();
    if (!$condition1) {
        $errors[] = 'Condition 1 failed';
    }

    if (!$condition2) {
        $errors[] = 'Condition 2 failed';
    }

    return $errors;
}

Now you can also take advantage of the fact that empty arrays are falsy:

$errors = check_stuff($your_stuff);
if (!$errors) {
    echo 'No errors!';
} else {
    print_r($errors);
}
like image 76
NullUserException Avatar answered Oct 04 '22 04:10

NullUserException