php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16223 Nested If block not execute correctly
Submitted: 2002-03-22 13:11 UTC Modified: 2002-03-22 13:49 UTC
From: amin dot javanbakht at openwave dot com Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 4.1.2 OS: SUN
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: amin dot javanbakht at openwave dot com
New email:
PHP Version: OS:

 

 [2002-03-22 13:11 UTC] amin dot javanbakht at openwave dot com
if ( ( $name != "test1" ) || ( $name != "test2" ) )

does not work properly.

if I Do each condition seperately, it works but If I join them using  ||   then this never works

however this works fine
if ( ( $name == "test1" ) || ( $name == "test2" ) )


it seems like the problem occurs only with "!=" condition

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-22 13:20 UTC] rasmus@php.net
Logic 101

If the fruit is not blue or the fruit is not red, do something.

Since the fruit can only be one colour, that logic will always return true.
 [2002-03-22 13:26 UTC] amin dot javanbakht at openwave dot com
>>If the fruit is not blue or the fruit is not red, do something.

The problem is when the fruit is blue, it still return true, but it has to return false
 [2002-03-22 13:49 UTC] daniel@php.net
What is $name supposed to be? Your examples (!= or ==) are applying a completely different logic.

$name = "foo";

If $name is not "test1" => TRUE

This Condition breaks at this point. The second condition is never executed.

Consider the following Script:
-------------------------------------------
<?php

function first() {
  echo "first<br>\n";
  return true;
}

function second() {
  echo "second<br>\n";
  return true;
}

if(first() || second())
  echo "YES<br>\n";
else
  echo "NO<br>\n";

?>
-------------------------------------------

The output is NOT "first, second, YES" as you might think. It is "first, YES". 

The || operator checks whether either the left condition or the right condition returns true. If the left condition is true, the right condition is not anymore checked.

Daniel Lorch
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 16:01:35 2024 UTC