Jump to content
Latest News: (loading..)
Sign in to follow this  
douglaswalker

&& 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 

Share this post


Link to post
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;
	    }
	 }

 

Share this post


Link to post
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


This is a signature that appears on all my posts.  
IF YOU MAKE A POST REQUESTING HELP...please state the exact version
of osCommerce that you are using. THANKS

 
Get the latest current code (community-supported responsive 2.3.4.1BS Edge) here

 

Share this post


Link to post
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.


If you are running the "official" osC 2.3.4 or 2.3.4.1 download, your installation is obsolete! Get (stable) Frozen or (unstable) Edge. See also the naming convention and the latest community-supported responsive "Edge" release

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×