This tutorial is for Logical Operators. You already have seen examples of
Assignment Operators In Php,
Arithmetic operators In Php,
Comparison operators In Php .We had assign operators and compared them but what if we want to use little bit of logic??? For example, to check few variables once and compared it with different values, best way to explain it is:-
Code for comparison operators(Screenshot1):-
|
Code for Comparison Operators Screenshot1 |
output (Screenshot2) :-
|
Screenshot2 |
In this example we have already seen comparing number1 and its true. Now what if you want to compare more variables,for that you have to take more “if statements”.Logical operators does work easy. Logical Operators are AND (&&), OR(||),NOT etc.In thistutorial we will look at OR, we can write in both way OR/|| and AND/&&.Lets see example for Logical operators to find upper bound and lower bound.
Code for AND logical operators (Screenshot3):-
|
Code for AND logical Operators Screenshot3 |
Ouput (Screenshot4):-
|
Screenshot4 |
In this example I have created variable upper value is 1000 and lower value is 500.I want to test whether specific number applied would be between values of lower and upper.So I want my variable number would be 1000 or 500 or something in between them.It can’t be 499 less then 500 and also it can’t be greater then 1000 like 1001.So my value of number is 654,but how can we check it if number is greater then or equal to lower && (AND) number is less then or equal to upper.So, It has to be greater then lower and less to from upper.The answer is ok because number is 654.
Code for AND logical operators (Screenshot5):-
|
Screenshot5 |
output (Screenshot6):-
|
Screenshot6 |
In this example number value we had changed to 999.999 and its echoing “OK” because its also less then 1000.
Code for AND logical operators (Screenshot7):-
|
Screenshot7 |
Output Screenshot8
|
Screenshot8 |
In this example number is greater then upper value it evaluates false to if statement and it will echo out number which must be between lower value and upper value.
Code for AND logical operators (Screenshot9):-
|
Screenshot9 |
Output Screenshot10
|
Screenshot10 |
In this its equal values, therefore it is ok.
Code for OR logical operators (Screenshot11):-
|
Screenshot11 |
|
Screenshot12 |
In this example we have variables such as number , canbe1 and canbe2. Now I want to compare number which is equal to either canbe1 or canbe2.If statement evaluates false not ok because number is not equal to value of canbe1 nor canbe2.
Code for OR logical operators (Screenshot13):-
|
Screenshot13 |
Output (Screenshot14):-
|
Screenshot14 |
In this example it will echo out “OK” if statement evaluates true.
Code for OR logical operators (Screenshot15):-
|
Screenshot15 |
Output (Screenshot16):-
|
Screenshot16 |
In this example it will echo out “OK” if statement evaluates true.
No comments:
Post a Comment
Thanks For Comment Will get you Soon..