Page 1 of 1

Bitwise OR logical operator

Posted: Sun Dec 16, 2007 1:44 am
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

Posted: Sun Dec 16, 2007 4:13 am
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)

Posted: Sun Dec 16, 2007 4:27 am
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

Posted: Sun Dec 16, 2007 12:30 pm
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.

Posted: Sun Dec 16, 2007 2:06 pm
by feyd
Technically, it may, but that would be a side effect of the bitwise operation being performed. Don't bank on side effects.