Changes

Jump to: navigation, search

PHP Operators

114 bytes added, 13:23, 31 May 2007
PHP Logical Operators
== PHP Logical Operators ==
''Logical Operators'' are also known as ''Boolean Operators'' because they evaluate parts of an expression and return a ''true'' or ''false'' value, allowing desicions to be made about how a script should proceed.
Perhaps the best way The first step to understand understanding how logical operators work is to construct a sentence rather than to look at a script example right away. Let's assume we want need to check some aspect of two variables named x $var1 and y$var2. Our sentence might read:
''If x $var is less than 10 25 AND y $var2 is greater than 20 45 display a message.''
Here the logical operator is the "AND" part of the sentence. If we were to express this in JavaScript we would use the comparision operators we covered earlier together with the && logical operator:
<pre>
if ((x $var1 < 1025) && (y $var2 > 2045)document.write ("Message")echo 'Our expression is true';
</pre>
Similarly , if our sentence was to read:
''If x $var1 is less than 10 25 OR y $var2 is greater than 20 45 display a message.''
Then we would replace the "OR" with the JavaScript equavalent ||:
<pre>
if ((x $var1 < 1025) || (y $var2 > 2045)document.write ("Message")echo 'Our expression is true';
</pre>
The final Logical Operator is the NOT operator which simply inverts the result of an expression. The ! character represents the NOT operatoer operater and can be used as follows:
<pre>
(10 > 1) // returns ''true''
!(10 > 1) // retruns returns ''false'' because we have inverted the result with the logical NOT
</pre>

Navigation menu