Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

&& combined with !=


Recommended Posts

Hi there,

I have come across a curious coding issue which may just be curious to me... but I wondered if any\one has come across it and could shed any light.

Ok here goes.... the code below

works as expected and stops the code running on checkout_shipping.php, checkout_payment.php and checkout_confirmation.php

 function isEnabled() {
    	global $PHP_SELF;
    	if (($PHP_SELF == 'checkout_shipping.php')|| ($PHP_SELF == 'checkout_payment.php') || ($PHP_SELF == 'checkout_confirmation.php'))  {
    	    $this->enabled = false;
    	}
  	return $this->enabled;
	}

Then i tried the code below to allow the code to run on checkout_shipping.php, checkout_payment.php and checkout_confirmation.php but no where else

however it does not work and the code runs nowhere.

 function isEnabled() {
    	global $PHP_SELF;
    	if (($PHP_SELF != 'checkout_shipping.php')|| ($PHP_SELF != 'checkout_payment.php') || ($PHP_SELF != 'checkout_confirmation.php'))  {
    	    $this->enabled = false;
    	}
  	return $this->enabled;
	}

then I tried this (see below and it works perfectly. Only allowing the code to run on  checkout_shipping.php, checkout_payment.php and checkout_confirmation.php

 function isEnabled() {
    	global $PHP_SELF;
    	if (($PHP_SELF != 'checkout_shipping.php')&& ($PHP_SELF != 'checkout_payment.php') && ($PHP_SELF != 'checkout_confirmation.php'))  {
    	    $this->enabled = false;
    	}
  	return $this->enabled;
	}

I thought that if I added && then all the conditions had to be correct at the same time?

Why does the || not work?

Any light that can be shed would be appreciated 

Link to comment
Share on other sites

If you want to use || this logic will suffice:

    function isEnabled() {
      global $PHP_SELF;
      
    	if (($PHP_SELF == 'checkout_shipping.php') || ($PHP_SELF == 'checkout_payment.php') || ($PHP_SELF == 'checkout_confirmation.php')) {
  	    return $this->enabled;
	    }
	 }

 

Link to comment
Share on other sites

Keep it more simple and allow the possibility to easier add pages in;

$pages = array('checkout_shipping.php', 'checkout_payment.php', 'checkout_confirmation.php');

if (!in_array($PHP_SELF, $pages)) {
  // is NOT a page in $pages
}

if (in_array($PHP_SELF, $pages)) {
  // IS a page in $pages
}

G

Link to comment
Share on other sites

Basic Boolean logic:  not(A or B) is the same as (not A) and (not B). Likewise not(A and B) is the same as (not A) or (not B).

By the way, == and != are higher precedence than || or &&, so the inner () can safely be omitted. Some coders prefer to explicitly give them just so things are perfectly clear to the next reader.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...