php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79604 OR and || returning different results with arrays
Submitted: 2020-05-15 17:04 UTC Modified: 2020-05-15 18:32 UTC
From: shariefjamiel at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.4Git-2020-05-15 (Git) OS: mac/ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: shariefjamiel at gmail dot com
New email:
PHP Version: OS:

 

 [2020-05-15 17:04 UTC] shariefjamiel at gmail dot com
Description:
------------
I noticed the OR operator was not working properly with a value inside an array,

Test script:
---------------
 /**
         * PHP 7.2.24-0ubuntu0.18.04.1 (cli) (built: Oct 28 2019 12:07:07) ( NTS )
         * PHP 7.4.5 (cli) (built: Apr 23 2020 02:25:56) ( NTS )
         * @see https://www.php.net/manual/en/language.operators.logical.php
         */
        $array = [
            'a' => 'php',
            'b' => false
        ];

        # EXPECTED BEHAVIOR
        $check = $array['b'] === true || $array['a'] === 'php'; // should and is true
        var_dump($check);

        # ACTUAL RESULT
        $check = $array['b'] === true or $array['a'] === 'php'; // should be true
        var_dump($check);
        
        # START SANITY CHECKS

        # Sanity Check 1
        var_dump(true === true or 'php' === 'php'); // should be and is true

        # Sanity Check 2
        $a = true;
        $b = 'php';

        var_dump($a === true or $b === 'php'); // should be and is true

Expected result:
----------------
bool(true)
bool(true)
bool(true)
bool(true)

Actual result:
--------------
bool(true)
bool(false)
bool(true)
bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-05-15 17:38 UTC] shariefjamiel at gmail dot com
-Status: Open +Status: Closed
 [2020-05-15 17:38 UTC] shariefjamiel at gmail dot com
ignore this been a long day, i think got my test script wrong.
 [2020-05-15 17:51 UTC] cmb@php.net
-Status: Closed +Status: Not a bug
 [2020-05-15 17:54 UTC] shariefjamiel at gmail dot com
Please delete my comment that i got my script wrong, its right. Sorry for confusion.
 [2020-05-15 18:05 UTC] shariefjamiel at gmail dot com
I have adjusted the sanity checks, and its still the same if you are using a boolean value set in an array and the OR , ||

/**
        * PHP 7.2.24-0ubuntu0.18.04.1 (cli) (built: Oct 28 2019 12:07:07) ( NTS )
        * PHP 7.4.5 (cli) (built: Apr 23 2020 02:25:56) ( NTS )
        * @see https://www.php.net/manual/en/language.operators.logical.php
        */
        $array = [
            'a' => 'php',
            'b' => false
        ];

        # EXPECTED BEHAVIOR
        $check = $array['b'] === true || $array['a'] === 'php'; // should and is true
        var_dump($check);

        # ACTUAL RESULT
        $check = $array['b'] === true or $array['a'] === 'php'; // should be true
        var_dump($check);
        
        # START SANITY CHECKS

        # Sanity Check 1
        var_dump(false === true or 'php' === 'php'); // should be and is true

        # Sanity Check 2
        $a = false;
        $b = 'php';

        var_dump($a === true or $b === 'php'); // should be and is true
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Jan 22 21:03:35 2022 UTC