Page 1 of 1
Question about bitwise and comparison operators...
Posted: Tue Sep 22, 2009 1:57 pm
by Wolf_22
What's the difference between '&' and "&&" (ampersands)?
i.e. -
Code: Select all
if ($type & SIMPLEPIE_TYPE_RSS_SYNDICATION)
Code: Select all
if ($type && SIMPLEPIE_TYPE_RSS_SYNDICATION)
Re: Question about bitwise and comparison operators...
Posted: Tue Sep 22, 2009 2:08 pm
by requinix
& is bitwise, && is boolean. Sometimes they have the same effect,
Code: Select all
0 & 0 = 0 false && false = false
0 & 1 = 0 false && true = false
1 & 0 = 0 true && false = false
1 & 1 = 1 true && true = true
sometimes they don't.
Code: Select all
0 == false, everything else == true
1 & 1 = 1
1 & 2 = 0 2 & 2 = 2
1 & 3 = 1 2 & 3 = 2 3 & 3 = 3
1 & 4 = 0 2 & 4 = 0 3 & 4 = 0 4 & 4 = 4
1 & 5 = 1 2 & 5 = 0 3 & 5 = 1 4 & 5 = 4 5 & 5 = 5
1 & 6 = 0 2 & 6 = 2 3 & 6 = 2 4 & 6 = 4 5 & 6 = 4
1 & 7 = 1 2 & 7 = 2 3 & 7 = 3 4 & 7 = 4 5 & 7 = 5
1 & 8 = 0 2 & 8 = 0 3 & 8 = 0 4 & 8 = 0 5 & 8 = 0
Bitwise
arithmetic is for numbers, boolean
comparisons are for conditions with true and false.
Re: Question about bitwise and comparison operators...
Posted: Tue Sep 22, 2009 2:40 pm
by Wolf_22
I'm working on a PHP 4 system, however, my development environment runs 5.3, which means that the call-by-reference operator (& or ampersand) is unnecessary. Because of this, the following throws me off:
Code: Select all
$categories_parent[] =& new $this->feed->category_class($term, $scheme, $label);
Is this a call-by-reference assignment?
Re: Question about bitwise and comparison operators...
Posted: Tue Sep 22, 2009 2:52 pm
by requinix
There's no "call-by-reference assignment" - you're mixing up a few terms.
Read up.
The deprecated feature is
call-time pass-by-reference. That's when you pass variables by-reference to a function when you call the function.
The code you have is perfectly fine: category_class returns a reference to a variable, but to keep that reference you need to use =& when you assign the result to a variable. In PHP 5 this is only necessary when dealing with things that aren't objects or resources, in PHP 4 you need it for objects too.
I strongly suggest you use the same version of PHP on your development machine as on the server.
Re: Question about bitwise and comparison operators...
Posted: Tue Sep 22, 2009 3:13 pm
by Wolf_22
tasairis wrote:There's no "call-by-reference assignment" - you're mixing up a few terms.
Read up.
The deprecated feature is
call-time pass-by-reference. That's when you pass variables by-reference to a function when you call the function.
The code you have is perfectly fine: category_class returns a reference to a variable, but to keep that reference you need to use =& when you assign the result to a variable. In PHP 5 this is only necessary when dealing with things that aren't objects or resources, in PHP 4 you need it for objects too.
I'll make sure to read everything from that link, but expect more questions.
tasairis wrote:I strongly suggest you use the same version of PHP on your development machine as on the server.
I agree, but it's easier said than done when it involves superiors.