"Jam Pa" wrote:
Visit <http://php.net/operators> and take a look at the sections on "Bitwise
operators" and "Logical operators"
--
phil [dot] ronan virgin [dot] net
http://vzone.virgin.net/phil.ronan/
Both '&' and '&&' are 'AND' right? Or what about 'ORs' '|' and '||'? Whats the difference? I not grok.......
Both '&' and '&&' are 'AND' right? Or what about 'ORs' '|' and '||'?
Whats the difference? I not grok....
"Jam Pa" wrote:
Visit <http://php.net/operators> and take a look at the sections on "Bitwise
operators" and "Logical operators"
--
phil [dot] ronan virgin [dot] net
http://vzone.virgin.net/phil.ronan/
"Jam Pa" <org> a écrit dans le message de news:
243.153.2...
Hi,
It takes me 1/4 seconds on Google :
http://www.php.net/manual/en/language.operators.bitwise.php
Bye,
> "Jam Pa" <org> a écrit dans le message de news: [/ref]
"\(¯`·..Yttrium ...·´¯\)" <fr> wrote in
news:429494a3$0$10517$free.fr:
Gee, you are quick. Good job, einstein.
Philip Ronan replied:
The thing is, I did already read the manual.
Does anyone have actual clearheaded answer instead of links and snappy
comments?
JP
Jam Pa <org> wrote in
news:243.153.2:
Here is an example test for those interested.
<?php
$a = "nil"; $b = "nil";
if (($a=true) | ($b=false)) { echo " * 1: true, a: $a , b: $b <br>"; }
$a = "nil"; $b = "nil";
if (($a=false) | ($b=true)) { echo " * 2: true, a: $a , b: $b <br>"; }
$a = "nil"; $b = "nil";
if (($a=true) || ($b=false)) { echo " * 3: true, a: $a , b: $b <br>"; }
$a = "nil"; $b = "nil";
if (($a=false) || ($b=true)) { echo " * 4: true, a: $a , b: $b <br>"; }
$a = "nil"; $b = "nil";
?>
output with comments
* 1: true, a: 1 , b:
* 2: true, a: , b: 1
- both evaluated above
* 3: true, a: 1 , b: nil
- only first one evaluated (it was true)
)
* 4: true, a: , b: 1
- both
Hmm, not sure if I'm really any wiser...
"Jam Pa" <org> kirjoitti
viestissä:243.153.2...
& and | are bitwise arithmetic operators while && and || are boolean
operators. What this maeans is that & and | handle single bits in and
integer, but && and || evaluate the entire integer as true or false.
For example we have and integer 4. when it is splitted in bits it looks like
0100
( = 0*(2^3) + 1*(2^2) + 0*(2^1) + 0*(2^0) )
And we have integer 8 that's 1000
( = 1*(2^3) + 0*(2^2) + 0*(2^1) + 0*(2^0) )
Now, comparing them (8 && 4) this returns TRUE, since both are none-zero
(there is at least one bit other than zero in both of them.
Bitwise operation on the other hand: 8 & 4 will return zero. This is because
it compares each single bit:
8 & 4 = 0
-----------
1 and 0 = 0
0 and 1 = 0
0 and 0 = 0
0 and 0 = 0
Now OR then. 4 || 8 returns TRUE, since within the two operators there is at
least one none-zero bit. Bitwise operation 8 | 4 returns 12. Let's see that
again in bitwise:
8 | 4 = 12
-----------
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0
0 or 0 = 0
because 1100 is the binary representation of 12. It's not the same as 8 + 4,
that was just a coincidence. 8 | 8 returns 8. :)
8 | 8 = 8
-----------
1 or 1 = 1
0 or 0 = 0
0 or 0 = 0
0 or 0 = 0
See the difference? When using && and || we just look if the integer is zero
or non-zero, but using & and | (and xor ^) we perform bitwise arithmetic.
AND and OR are not the same as && and ||, but they are closer to them, than
to & and |. I never understood completely the difference, but there is a
section in the manual that trys to explain it, but I just can't find it now.
All I found was this: "The reason for the two different variations of "and"
and "or" operators is that they operate at different precedences. " (PHP
Manual). Operator precendence is all about in what order the expression is
evaluated.
--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears
com
"Kimmo Laine" <com> wrote in
news:d727g3$9e$kolumbus.fi:
[a LOT deleted]
Thank juu Kimmo! Jotain tollaista mun kokeilutkin indikoi! :) Salatiedettä!
So, what should one use when comparing in if clauses or ternary ? : ops
when evaluating the validity of user input, for instance?
Is this better?
if ($gee && $whiz) { //do shiz)
or perhaps
if ($gee AND $whiz) { //do... }
Jp
"Jam Pa" <org> kirjoitti
viestissä:243.153.2...
> [a LOT deleted]
>
> Thank juu Kimmo! Jotain tollaista mun kokeilutkin indikoi! :)
> Salatiedettä![/ref]
Eipä kestä...
Seems to me that in that particular case there is no difference. Reading the
manual I found an example that sort of demonstrates the effect of OR.
$var0 = "";
$var1 = "bunny";
$default = "default";
$target = $var0 || $var1 || $default;
echo $target; //outputs 1
$target = $var0 OR $var1 OR $default;
echo $target; //outputs bunny
Why "bunny"? Seems that using or the operand is assigned into $target, and
if it's true, it halts. It is evaluated like this:
(($target = $var0) || $target = $var1) || $target = $default;
$target = $var0 is false, since $var0 is empty, but $target = $var1 is true
("bunny" is true) so it halts, since "true or something" is always true, it
"won't bother" to check $default.
--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears
com
Kimmo Laine wrote:
<...SNIP...>
No it doesn't. The result is an empty string.
No it's not. The line is evaluated as:
($target = $var0) || $var1 || $default;
--
Oli
"Oli Filth" <co.uk> kirjoitti
viestissä:7R2le.34$ntli.net...
> <...SNIP...>
>
> No it doesn't. The result is an empty string.
>
>
> No it's not. The line is evaluated as:
>
> ($target = $var0) || $var1 || $default;
>[/ref]
Ah... So it does, I had to test it. Well in that case the user comment at
php.net was incorrect... or I understood it wrong... :(
--
"I am pro death penalty. That way people learn
their lesson for the next time." -- Britney Spears
com
Bookmarks