Bitwise OR logical operator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

Bitwise OR logical operator

Post by thatsme »

This is the script given in one of the popular website

Code: Select all

if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
die('You did not complete all of the required fields')
I think, It should have been

Code: Select all

(||)
between each

Code: Select all

$_POST


Or we can use anyone of them?
i am confused.

here is the link,

http://php.about.com/od/finishedphp1/ss ... code_2.htm
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

| is bitwise operator but || is logical operator.
I also think that || should be there and tried it with some examples but amazing | also worked in the situation.
That makes me confusing too 8)
User avatar
seppo0010
Forum Commoner
Posts: 47
Joined: Wed Oct 24, 2007 4:13 pm
Location: Buenos Aires, Argentina

Post by seppo0010 »

It´s a weird way to do it , but it works fine... the ! operator cast all vars as boolean, and then the bitwise cast them as integers, so you´ll have a 1 if a var is empty (null, 0, "", "0"), and a 0 if all of them are not empty
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Post by Chalks »

I thought that one pipe "|" forced the if statement to check _every_ test, regardless of when a TRUE value was found, and the double pipe "||" would only evaluate the statements until the first TRUE value is found. Same with a single ampersand versus a double ampersand. So, if you have a function in you if statement that you _must_ have evaluated, use the single pipe/ampersand.


I think. Maybe I'll go test it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Technically, it may, but that would be a side effect of the bitwise operation being performed. Don't bank on side effects.
Post Reply