PHP Either/Or

Techno Child

web{designer}
Joined
10 Oct 2004
Messages
871
How exactly would this be done?

I realize that this basically means if both conditions are true, then continue... How would I set this up so that if either one is true, the script will continue?

Code:
if ($a == "a" || $b == "b") {

continue

}else{

dont continue

}
thanks
-tc


EDIT: oh yeah... it is pretty important that I do not split this up into two if/else statements
 
how does that not work?

if ($a == "a" && $b == "b")
thats if both are true
(btw you can use OR and AND)
 
and only continues if both are true, he wants it to continue on all 3 eventualities. You are better off using logical OR rather than bitwise or (||)
 
what 3 eventualities? what am I looking at, it only has 2 conditions
 
the third eventuality would be if both were true...

anyways, I was not aware that I could use "AND" or "OR" call me a noob :(

thanks guys


EDIT:

Lordofla said:
You are better off using logical OR rather than bitwise or (||)

what would be the bitwise AND? just curious...
 
I'm not sure I understand the original question either. The code in the first post was:

Code:
if ($a == "a" || $b == "b")
  // continue
else
  // don't continue
What that says is: if ($a == "a") is true OR ($b == "b") is true OR both conditions are true, then execute whatever is in the "continue" block. The code in the "don't continue" block is only executed if neither of the conditions are true (i.e. BOTH conditions are false). Isn't that what you want?

Also note that short-circuit evaluation applies to the logical OR operator (||) that you're using. What that means is if ($a == "a") evaluates to true, then the code in the "continue" block will be executed regardless of what the ($b == "b") condition evaluates to.
In other words, if the first condition in a logical OR evaluates to TRUE, the second condition is basically ignored and the whole expression evaluates to TRUE. If the first condition evaluates to FALSE, then the whole expression evaluates to whatever the second condition evaluates to. If I understand your question correctly, this shouldn't matter for your situation though.
 
Last edited:
OK currently you have it setup to continue if either or both of your conditions are true to continue. Are you wanting it so that it only continues if its one or the other and not both true?

PHP Logical Operators: You might want to look at 'xor'.

PHP:
if ($a == 'a' xor $b == 'b') {
    // continue
} else {
    // dont continue
}
 
Actually I needed it to continue if either one or both is true... The example I have demonstrated was the current codding for a simple login script which shows/hides the rest of the page depending on the user's credentials. What I needed to add was a line that checked for a cookie that gets set on the previous page, however I fixed it by separating the login form from the actual content page.

This info is good to know though, thanks for the link Geffy... Being self taught, I had no idea that these were called logical operators, so I couldn't look it up on their site.

-tc
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back