Ive seen “||” being used in some code, hope this isnt a dumb question but is it the same thing as “or”?
No.
Yes they both mean “or”, but they are used in different statements
I don’t know the full answer to this but may this will help you out
You use “OR” in mysql queries …
[php]$sql = mysql_query(“SELECT * FROM table WHERE id=‘1’ OR id=‘2’”);[/php]
You use “||” in if() statements or the like…
[php]if($id==1 || $id==2){
// Do something;
}[/php]
Actually, this is not a dumb question at all!
While you can usually use the two interchangeably in php, there is a notable difference between the two. (The same for && and AND)
Logically they are identical, but they have different precedence compared to other operators. Consider the following code:[php]$test1 = 2 OR false;
$test2 = 2 || false;
$test3 = 2 AND false;
$test4 = 2 && false;[/php]
If they were identical, the results should be the same for the first two tests, and the same for the last two, but if you run this the results are actually:[php]
$test1 = 2
$test2 = 1
$test3 = 2
$test4 = [/php]
Note that the difference between them is the precedence with regard to not only each other but the assignment operator (=).
Here is the actual precedence for the five operators used above: “&&” > “||” > “=” > “AND” > “OR”
Thanks guys. Wow malasho. Im still trying to wrap my head around how precedence works. thank you so much for the explanation!
My pleasure, I find that I usually use only the symbolic operators (|| and &&). By using just one combination, I always know how they will interact and can structure my logic accordingly. Sometimes I’ll be working with another person’s code and if it is written with the word forms instead, I can about tear my hair out before I remember that they have a higher precedence than I am used to.
Just knowing and remembering that there is a precedence to all operators is helpful!
Let us know if you have any other questions.
jay