This tutorial is about Comparison Operators.Now “Comparison Operator” is basically to compare one or more values. We can compare any amount of values in a single statement.In this tutorial we will compare three values via demonstration.Now for this example, we are going to use “if statement” , I had shown you example about If Statement in my previous tutorials .
code for if Statement Condition true (Screenshot1):-
Screenshot1 |
<?php $number1 = 10; $number2 = 9; if (true) { echo 'inside.'; } ?>
Output (Screenshot2) :-
Screenshot2 |
In this code, condition evaluates either true or false. If I write true it will execute it and inside the browser it will display it and if I will write false it will not execute inner side of “if statement” example.
code for if Statement Condition false (Screenshot3):-
Screenshot3 |
<?php $number1 = 10; $number2 = 9; if (false) { echo 'inside.'; } ?>
output (Screenshot4) :-
Screenshot4 |
In this code it automatically evaluates “False” by false keyword. Now its same as we do 1==1. It just contain “double equal to” sign to compare variables. Now always remember when we assign values we use single equal sign (=) and in comparison we use double equal to sign (==) for comparing two variables.
Code for comparison operators Example (Screenshot5):-
Screenshot5 |
<?php $number1 = 10; $number2 = 9; if (1==1) { echo 'inside.'; } ?>
Output (Screenshot6):-
Screenshot6 |
In this example we are comparing variables such as number one is equal to number one, condition is true so,it will echo out the result as you seen in screenshot6.
Code for comparison operators Example (Screenshot7):-
Screenshot7 |
<?php $number1 = 10; $number2 = 9; if (1==2) { echo 'inside.'; } ?>
Output (Screenshot8):-
Screenshot8 |
In this example number one is not equal to number two then it will evaluate false condition and will not echo out anything as result as u seen in (screenshot 8).
Other Comparison Operators Examples
Code for comparison operators Example (Screenshot9):-
Screenshot9 |
<?php $number1 = 10; $number2 = 9; if (1!=2) { echo 'inside.'; } ?>
output (Screenshot10):-
Screenshot10 |
In this example we are using exclamation mark (!) which means not number one is not(!) equal to number two, then it evaluates true which will echo out as result.
Code for comparison operators Example (Screenshot11):-
Comparison Operators Screenshot11 |
<?php $number1 = 10; $number2 = 9; if (1!=1) { echo 'inside.'; } ?>
output (Screenshot12):-
Screenshot12 |
In this example we are using exclamation mark (!) which means not number one is not(!) equal to number one, then it evaluates false which will echo out as result.
Code for comparison operators Example (Screenshot13):-
Screenshot13 |
<?php $number1 = 10; $number2 = 9; if($number1>$number2) { echo 'Yes.'; }else { echo 'No.'; } ?>
Output Screenshot14
Screenshot14 |
In this example we are using greater than(>) and smaller than(<)comparison operators. Now in this example, condition is if number1 is greater then number 2 then echo out yes or else no, but condition is true so it will echo out yes.
Now Try Some Examples with yourself giving you code copy and paste see results :-
Code for Example 1 (Screenshot15):-
Screenshot15 |
<?php $number1 = 10; $number2 = 9; if($number1<$number2) { echo 'Yes.'; }else { echo 'No.'; } ?>
Code for Example 2 (Screenshot16):-
Screenshot16 |
<?php $number1 = 10; $number2 = 9; if($number1>=$number2) { echo 'Yes.'; }else { echo 'No.'; } ?>
Code for Example 3 (Screenshot17):-
Screenshot17 |
Code for Example 4 (Screenshot18):-
Screenshot18 |
Code for Example 5 (Screenshot19):-
Screenshot19 |
Now comparison operators are very important when you work dynamically on website. For example, you want to submit form to check whether password is correct or not
Code for comparison operators (Screenshot20) :-
Screenshot20 |
Output (Screenshot21):-
Screenshot21 |
In this example we are matching password is correct or incorrect so, you can see its pretty easy and very useful to check conditions are whether True or False.In this, it will evaluate true as it matches variable values.
Code for comparison operators (Screenshot22) :-
Screenshot22 |
output (Screenshot23):-
Screenshot23 |
It will mismatch condition and evaluate false and will echo out incorrect .
No comments:
Post a Comment
Thanks For Comment Will get you Soon..