php - Will it make any diffrence to my script if i use && instead of AND?

150

Can any one tell, will it make any difference if I use && operator for condition instead 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.

339

Answer

Solution:

The difference between&& andand 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)
57

Answer

Solution:

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

884

Answer

Solution:

php being the frankenlanguage that it is, I would personally fall back on my General Orders:

  1. If you are maintaining an existing code base, determine the coding standard inherent within it(if there is one)
  2. 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)
  3. If you are writing new code, establish a coding standard, and use that.
953

Answer

Solution:

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.

People are also looking for solutions to the problem: php - Getting Google Maps URL? :)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.