09.22.07
Logical Operators Used in PHP
Logical Operators Used in PHP
In a previous article, two important groups of operators were covered. The arithmetic group of operators which consisted of obtaining a number result through the manipulation of two or more previous numbers and the comparison group of operators which were used to compare one number to another in a variety of different ways. There is a third and all important group of operators known as the logical operators and indeed regardless of the type of programming that you do you are probably going to have to use logical operators at some point. This is why they have been given their own article, so that each of the three can be explored deeper.
The first of the three is the && operator, which simply stands for the word and. If you want more than one thing to be signified in any part of any script that you write, you are going to use the && logical operator.
Here is an example:
if x = 1
and y = 2
then (x<2 && y<2) would return false, but (x<2 && y<3) would return true.
Another operator that you need to concern yourself with is the || operator. This symbol stands for the word or and just like and it is used in a number of different places within the PHP script. If you want one of a group of things to be selected or to be true but you don’t necessarily require the others in the same group to also be selected or true, then you would use the or operator instead of the and operator.
Here is an example:
If x = 1
And y = 2
Then (x<2 || y<2) would return true instead of false as was the case in the previous example where the && operator was used.
The third operator of importance from the logical standpoint is the ! operator, which stands for the word not. Not is used in a somewhat different way than the other two logical operators because you are specifically using it when you want something to either not be correct or alternatively not be chosen.
Here is an example:
If x = 1
And y = 2
Then !(x==y) would return true, but !(x!=y) would return false. Notice that the second example returns false because of the double negative that exists in the statement. If x is not equal to y, then the statement becomes x is equal to y. Since x is not equal to y, the statement is false.
Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.