Can any one tell, will it make any difference if I use && operator for condition instead开发者_如何学JAVA of and in my php script?
For e.g
if($i == 1 and $bool == true)
is same as
if($i == 1 && $bool == true)
It will be better if anyone tell me difference between them.
The difference between &&
and and
is precedence: &&
has a higher one.
If you evaluate boolean expressions, I would stick with the most common used: &&
and ||
.
Update:
Example:
a || b and c
evaluates as
(a || b) and c
whereas
a || b && c
evaluates as
a || (b && c)
The only difference is operator precendence, i.e if you mix and match types which ones are evaluted first.
See http://www.php.net/manual/en/language.operators.logical.php
php being the frankenlanguage that it is, I would personally fall back on my General Orders:
- If you are maintaining an existing code base, determine the coding standard inherent within it(if there is one)
- If you are maintaining an existing code base with no apparent coding standard, establish one and as you work through the code base make the code adhere to the new coding standard (do not, however, modify code that is working bug free)
- If you are writing new code, establish a coding standard, and use that.
There is precedence issue only about using whether && or And, Moreover i recommend you to use && in numerical type comparisons and phrase comparisons "And", "Or". These make more sense to me. they'll increase the readability in your code.
精彩评论