How do I read this php statement?

return $stmt->fetch() !== false;

How do i read this the above php statement? the “!== false” confuses me.

if a record is found an array is returned else false.
is this not enough? why to i need “!== false”???
**return $stmt->fetch() **

Is this not correct?

$result = $stmt->fetch();
if ($result)
   return $result;
  return false;

The “!== false” REALLY confuses me.

It says return true if the fetch doesn’t return false. It’s not really returning the result of the fetch. It returns the evaluation of that condition.

Am still not getting it.
what is an example of that? that does not use a result set? where can i read about this kinda usage?

Hi there, needed a bit more time to reply to you in detail just then…

So when you do the following:

return $stmt->fetch() !== false;

You’re just returning the evaluation of a condition which would be either true or false.

On the other hand the following:

return $stmt->fetch() || false;

Means return the results of the fetch function and if they are null or any falsy value then return false;

But why not just:?

return $stmt->fetch();

It depends on the code that implements that function basically… Maybe giving more context like an example of a piece code that uses this code and the purpose of it can help up us in helping you out.

Am feeling dense… This is from a tutorial I am doing
you say evaluation, but it is not inside an if/else statement, so i guess that is what is confusing me. lol
The manual says a fetch returns an array or false.

    public static function emailExists($email)
        $sql = 'SELECT * FROM users WHERE email = :email';

        $db = static::getDB();
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':email', $email, PDO::PARAM_STR);


        return $stmt->fetch() !== false;

Because the function is called emailExists then it makes sense. It’s returning true if fetch gives a result and returning false if it doesn’t. If fetch returns null instead of false then this function would still return false keeping the type a boolean strictly speaking :slight_smile:.

So why not just then return $stmt->fetch();?

1 Like

So this is the equivalent? much easier on my eyes and head. lol

$result = $stmt->fetch();
if ($result !== false)
   return  $result; //( if var_dump an array with data )
   return false;
        //if record is found this returns true 
        $x = $stmt->fetch() !== false; //Return TRUE  

        //if record is found this returns array 
        $x = $stmt->fetch(); //array

The last post hits the nail on the head. $stmt->fetch() returns an array, so when you return that from your emailExists function then that function would have a mixed return type, namely an array or false.

Generally functions with mixed return types are hard to work with, because every time you call them you have to inspect what they’ve returned to you and what that means. Whereas if you return $stmt->fetch() !== false then you always get a boolean return from that function, which is a lot easier to work with.


Perhaps the examples from the PHP manual regarding the ambiguous return strpos(…) return values may be of interest and also be helpful.

1 Like

Based on the method name, emailExists, it is looking for a Boolean result and not the actual email for use, so let the DB tell you and then just return the fetch.

SELECT EXISTS (SELECT 1 FROM users WHERE email = :email)

My question though, especially that this code is from a tutorial, is WHY is the existence of an email being checked? This is generally done when doing a user registration which builds in a race condition. In that case the proper method would be to set a unique constraint on the email/username column. Attempt the insert, and then catch the duplicate error if any.

On your first example, the if and the else would be unnecessary. The first return basically acts as an exit or die. More importantly, $result is already going to be true (with a result) or false so there is no need for a redundant false check.

for your info the tutorial name is “Part 2 - Build a Complete Registration and Login System using PHP MVC”. Am new to frameworks and OOP

by the way I agree with you

a redundant false check.

I kind get how that paragraph works now… i just never seen a false check done that way and was trying to understand it

John’s link touches on it. Maybe looking at the strict vs. loose comparison chart will enlighten some.

For some languages, when the value of an Int is zero, it’s an Int. For PHP a zero can be a zero, but when loosely compared a zero can evaluate to a Boolean false. eg. if you don’t want to risk code seeing a zero as a false, use strict comparison.

Similar concerns with nulls and empty strings. If it matters, be sure your conditionals test what you think they’re testing.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.