• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

PHP Either/Or

#1
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
 

Khayman

I'm sorry Hal...
Political User
#2
how does that not work?

if ($a == "a" && $b == "b")
thats if both are true
(btw you can use OR and AND)
 
#5
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...
 
#8
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:
#9
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
}
 
#11
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 posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,013
Latest member
Pdawgintown