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 4616384
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:58:25+00:00 2026-05-22T01:58:25+00:00

Does anyone know a reliable way to distinguish between FALSE and NULL with PHPUnit?

  • 0

Does anyone know a reliable way to distinguish between FALSE and NULL with PHPUnit?

I’m trying to distinguish from NULL and FALSE in return values in my assertions.

This fails:

$this->assertNotEquals(FALSE, NULL);

And these assertions pass:

$this->assertFalse(NULL);
$this->assertNull(FALSE);

Edit: For some context, this is to distinguish between an error state (FALSE) versus an empty result (NULL). To ensure the function is returning properly, I need to distinguish between the two.
Thanks

Edit…
As per some of the problems regarding what I am testing, I’m adding the tests.

Class testNullFalse extends PHPUnit_Framework_TestCase{


    public function test_null_not_false (){
      $this->assertNotEquals(FALSE, NULL, "False and null are not the same");
    }

    public function test_null_is_false (){
      $this->assertFalse(NULL, "Null is clearly not FALSE");
    }

    public function test_false_is_null (){
      $this->assertNull(FALSE, "False is clearly not NULL");
    }

    public function test_false_equals_null(){
      $this->assertEquals(FALSE, NULL, "False and null are not equal");
    }

    public function test_false_sameas_null(){
      $this->assertSame(FALSE, NULL, "False and null are not the same");
    }

    public function test_false_not_sameas_null(){
      $this->assertNotSame(FALSE, NULL, "False and null are not the same");
    }
}

And the results.

PHPUnit 3.5.10 by Sebastian Bergmann.

FFF.F.

Time: 0 seconds, Memory: 5.50Mb

There were 4 failures:

1) testNullFalse::test_null_not_false
False and null are not the same
Failed asserting that <null> is not equal to <boolean:false>.

2) testNullFalse::test_null_is_false
Null is clearly not FALSE
Failed asserting that <null> is false.

3) testNullFalse::test_false_is_null
False is clearly not NULL
Failed asserting that <boolean:false> is null.

4) testNullFalse::test_false_sameas_null
False and null are not the same
<null> does not match expected type "boolean".

FAILURES!
Tests: 6, Assertions: 6, Failures: 4.
  • 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-22T01:58:26+00:00Added an answer on May 22, 2026 at 1:58 am

    These assertions use == which will perform type coercion. Hamcrest has identicalTo($value) which uses ===, and I believe PHPUnit has assertSame($expected, $actual) which does the same.

    self::assertSame(false, $dao->getUser(-2));
    

    Update: In answer to your comment, “It can be NULL or an object”:

    $user = $dao->getUser(-2);
    self::assertTrue($user === null || is_object($user));
    

    Using Hamcrest assertions is a little more expressive, especially in the event of a failure:

    assertThat($dao->getUser(-2), anyOf(objectValue(), nullValue()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a reliable way to validate International Bank Account Number (IBAN)
Does anyone know of a reliable way to store data to use across pages
Does anyone know a reliable way to check whether the referrer of a HTTP
Does anyone know how I can, in platform-independent C++ code prevent an object from
Does anyone know the range comparison of the BETWEEN clause? if I have a
Does anyone know is there a better way to check if some image contain
Does anyone know how to select entries from SQL table where first record from
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone know if it is possible to reliably determine (programattically C/C++...) whether or
Does anyone know how to transform a enum value to a human readable value?

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.