Page 1 of 1
Do I need parenthesises?
Posted: Fri Apr 06, 2007 1:02 am
by SmokyBarnable
This seems to work fine but I was wondering if I should use parenthesises?
or
Thanks.
Posted: Fri Apr 06, 2007 1:15 am
by aaronhall
The extra parentheses affect only the
order of operations (in this case, not at all) -- they aren't syntactically required
Posted: Fri Apr 06, 2007 6:21 am
by Benjamin
For readability..
Code: Select all
if (($hours === 1) && ($minutes < 30))
{
// blah blah
// blah
}
Posted: Fri Apr 06, 2007 6:49 am
by Oren
This is too simple, you don't need them.
Re: Do I need parenthesises?
Posted: Fri Apr 06, 2007 7:42 am
by lanasa
The reason you don't need them is the '&&' operator has more precidence than the < or === operators.
Posted: Fri Apr 06, 2007 8:11 am
by feyd
&& has a lower precedence, not higher.
I prefer to use the "and," "or" and "xor" keywords more than the "&&" and "||" operators because they have an even lower precedence. When I need to take the operation up a bit in the precedence, then I use the symbolic logicals.
http://php.net/language.operators#langu ... precedence
Posted: Fri Apr 06, 2007 8:56 am
by bubblenut
To add to feyds point on using keywords, take the following example.
Code: Select all
$val1 = 1; $val2 = 2;
if( $val1 = $val2 && true ) {}
var_dump( $val1 );
You will notice that $val1 is bool(true) rather than int(2). This is because the precedence of = is lower than that of && so effectively the statement is this
however, when using keywords instead the precedence of = is higher. With the following example you will notice that the result is int(2)
Code: Select all
$val1 = 1; $val2 = 2;
if( $val1 = $val2 and true ) {}
var_dump( $val1 );
[edit]I spell like a 2 year old[/edit]