What is the difference between & and ?

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
aditya
Forum Newbie
Posts: 15
Joined: Mon Jan 15, 2007 7:44 am

What is the difference between & and ?

Post by aditya »

Hi

What is the difference between & and ? in PHP
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Can you give an example? Each have many different purposes.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

&, used alone, is a bitwise operator.

? can be found being used as the ternary operator (scroll down).

When combined with other characters, these can take on different meanings. For instance, && is a logical and.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"&" as a unary operator is a reference indicator. "&" as a binary operator is a bitwise comparitor. "&&" is a logical comparitor. "?" is half of the ternary operator.

Examples:

Code: Select all

$foo = &$bar; // foo is now a reference to $bar, not a copy.
$foo & $bar; // $foo and $bar are compared in binary. The bits shared between them are returned.
$foo && $bar; // $foo and $bar are compared logically. If $foo is true and $bar is true, true is returned. All else returns false.
$foo ? $bar : $baf; // If $foo is true, $bar is returned, otherwise $baf is returned.
Post Reply