Page 1 of 1

What is the difference between & and ?

Posted: Wed Dec 12, 2007 9:12 pm
by aditya
Hi

What is the difference between & and ? in PHP

Posted: Wed Dec 12, 2007 9:28 pm
by John Cartwright
Can you give an example? Each have many different purposes.

Posted: Wed Dec 12, 2007 10:37 pm
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.

Posted: Thu Dec 13, 2007 10:28 am
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.