Question about bitwise and comparison operators...

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
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Question about bitwise and comparison operators...

Post 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)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Question about bitwise and comparison operators...

Post 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.
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Question about bitwise and comparison operators...

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Question about bitwise and comparison operators...

Post 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.

Code: Select all

func(&$variable);
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.
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Question about bitwise and comparison operators...

Post 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.

Code: Select all

func(&$variable);
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.
Post Reply