Jump to content

OR statement with multiple AND statements


ohno

Recommended Posts

We never established what version of PHP you are using?  

Perhaps these few fundamentals will help you understand:

  • PHP has arrays.  An array is like a toolbox that has compartments.  With PHP these compartments can be named ie the 'cartitem' in $_POST['cartitem'].
  • You are using these arrays:  $_POST which is the contents of a form that has been submitted, and $_SESSION which is a special array that stores variables on the server associated with a client/web browser.
  • If you try and access a named array element (example:  $_POST['cartitem']) and that array element has not been set with a value, PHP will generate an error.  This is why you are checking first with isset().  isset let's you code around the possibility a variable will not be set.

In your code, despite the fact that you think the session variables would have previously been set, obviously there are times when they aren't.  For example, if I open a browser and go directly to your post page, the session variables won't be set.   

Personally speaking, big chains of and/or combinations are ugly and hard to maintain.  Current best practices are to do early return when possible on individual problems, but in order to understand how to implement that we would need to see more than the if condition.  What is done/not done given success or failure? 

This explains why Barand's code is so much cleaner and simpler, as it uses an operator '??" called the "null coalescing" operator, that helps make this whole isset chaining of code obsolete, as it makes it simple to assign optionally assigned or passed variables a default value.  It was added in PHP7 however, so if you are on a version prior to 7, then it would explain why it would not work. 

With that said, I would rewrite things so that ALL the required variables are set to known values if they fail isset.

Link to comment
Share on other sites

Initializing without php7.

$_SESSION['cartid'] = isset($_SESSION['cartid']) ? $_SESSION['cartid'] : 0;
$_POST['cartitem'] = isset($_POST['cartitem']) ? $_POST['cartitem'] : 0;
$_POST['quantity']) = isset($_POST['quantity']) ? $_POST['quantity']) : 0;
$_SESSION['lockedcard'] = isset($_SESSION['lockedcard']) ? $_SESSION['lockedcard'] : 0;
$_SESSION['lockedpaypal'] = isset($_SESSION['lockedpaypal']) ? $_SESSION['lockedpaypal'] : 0;

// Now you can omit any isset() calls, and concentrate on the if/else conditions of the variables.

 

Link to comment
Share on other sites

7 hours ago, ginerjm said:

Looking at your last set of code I see you will still have the same problem with the lockedcard part.  You need to use isset on it before trying to see if it has the value you are looking for.

And - no - I don't help someone if I don't know what I am doing.  I politely say 'No - I can't help you.'  and leave it at that.

Well I guess that sums it up! What I posted works as far as O can see (perhaps you could be more helpful and state what won't work?). I came here for help. I don't help someone if I don't know what I'm doing. I DO help someone who doesn't know what they are doing but I do! Sorry. But in my day that's what forums were all about.

Link to comment
Share on other sites

6 hours ago, gizmola said:

Initializing without php7.


$_SESSION['cartid'] = isset($_SESSION['cartid']) ? $_SESSION['cartid'] : 0;
$_POST['cartitem'] = isset($_POST['cartitem']) ? $_POST['cartitem'] : 0;
$_POST['quantity']) = isset($_POST['quantity']) ? $_POST['quantity']) : 0;
$_SESSION['lockedcard'] = isset($_SESSION['lockedcard']) ? $_SESSION['lockedcard'] : 0;
$_SESSION['lockedpaypal'] = isset($_SESSION['lockedpaypal']) ? $_SESSION['lockedpaypal'] : 0;

// Now you can omit any isset() calls, and concentrate on the if/else conditions of the variables.

 

I'll take a look Monday. PHP 7.3 btw. Thanks :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.