Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5975125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:02:21+00:00 2026-05-22T21:02:21+00:00

Just a quick question – and I’m sure really basic! I have the following

  • 0

Just a quick question – and I’m sure really basic!

I have the following code:

function checkThings($foo, $bar) {
    ...
    if ($valid) {
        return $results;
    } else {
        return false;
    }
}

On the other end of this I am currently doing

$check = checkThings($foo, $bar);
if ($check === false) {
    echo "Error";
} else {
    echo $check;
}

Is writing the following the same?

$check = checkThings($foo, $bar);
if (!$check) {
    echo "Error";
} else {
    echo $check;
}

Which method is the preferred method if both are correct?

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:02:22+00:00Added an answer on May 22, 2026 at 9:02 pm

    The triple-equal operator is type-sensitive. So when you check:

    if ($check === false)
    

    … it will only be true if $check is the boolean value “false”. Wheras

    if ($check == false)
    

    … is not checking specifically for boolean false, but a “falsey” value. False, in PHP, equals zero, null is “falsey”, as is an empty string ("" == false == null, but "" !== false !== null). So:

    $check = 0;
    if ($check == false)
    

    … evaluates to true.

    The ! prefix operator is equivalent to ==. So when zero needs to be a discrete value from boolean false, the ! and == operators are not sufficient.

    Check out the docs for comparison operators here: http://php.net/manual/en/language.operators.comparison.php

    It is best practice to make your conditional checks as specific as possible. This is not only to avoid potential oversights in your logic, but also to make the code more maintainable for future developers. Imagine encountering a line of code like this:

    if (check_something($variable)) {
      // do stuff
    }
    

    I can assume the check_something function is returning a boolean value true. But, unless I go dig up the check_something function, it could also be returning an non-empty string, a number… who knows! Much more clear to do this:

    if (check_something($variable) === true) {
      // do stuff
    }
    

    Now, just by looking, I know that the check_something function is expected to return a true value. I might not know what the function does, but at least it is exactly clear what it returns. Another common example you see EVERYWHERE:

    if (!$_GET['value']) {
      // do something
    }
    

    This is a pet peeve. A conditional statement should always be comparing things clearly. So, you’d want to do:

    if (array_key_exists('value', $_GET) !== false && $_GET['value'] === '1') {
      // do something
    }
    

    Now, you can tell that I am not only checking to see if the query string parameter exists, but also whether it is equal to a specific value.

    In summary, the single ! prefix operator and the == operator are rarely useful and always ambiguous. You’re best served by writing your code in a way that documents itself, or is translatable into human language to express the logic at play. A direct comparison using either !== or ===, where appropriate, is a good habit, good practice, and the least likely to produce unexpected results in your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick question... I currently have the following jQuery code with a selector
just a quick question. I have a function called multiple times with a 5
just a quick question: I am a CS undergrad and have only had experience
just a quick question, if I have a matrix has n rows and m
Just a quick question here. I have a program in which I need to
Just a quick question. When I use the _() function provided by the Pylons
Just a quick question to make sure i'm on track - I've started supporting
Just a quick question on working with International Timezones & GMT. I have an
just a quick question : var h = $('#hebergeurJQUERY').val(); var t = $('#typeJQUERY').val(); function
Just a quick question to confirm the documentation: The getY(int) function from MotionEvent in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.