PHP Either/Or

Techno Child

web{designer}
Joined
Oct 10, 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
 

Khayman

I'm sorry Hal...
Political User
Joined
Jan 6, 2002
Messages
5,518
how does that not work?

if ($a == "a" && $b == "b")
thats if both are true
(btw you can use OR and AND)
 

LordOfLA

Godlike!
Joined
Feb 2, 2004
Messages
7,026
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 (||)
 

Khayman

I'm sorry Hal...
Political User
Joined
Jan 6, 2002
Messages
5,518
what 3 eventualities? what am I looking at, it only has 2 conditions
 

Techno Child

web{designer}
Joined
Oct 10, 2004
Messages
871
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...
 

NetRyder

Tech Junkie
Joined
Apr 19, 2002
Messages
13,256
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:

Geffy

OSNN Veteran Addict
Joined
Mar 18, 2002
Messages
7,805
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
}
 

Techno Child

web{designer}
Joined
Oct 10, 2004
Messages
871
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

Perris Calderon wrote on Electronic Punk's profile.
Hey ep!

All good with me, applying for microsoft mvp right now, should have done this a while ago.

Notifications don't work, I only found your response by comming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier, mine is perriscalderon at gmail
Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...

Forum statistics

Threads
61,974
Messages
673,294
Members
89,042
Latest member
Pancake