Tuesday, 25 February 2014

Comparison operators in Php

Comparison Operators php
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):-

comparison operators in php
Screenshot1
<?php
$number1 = 10;
$number2 = 9;
if (true) {
echo 'inside.';
}
?>

Output (Screenshot2) :-

if statement condition ture
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):-

comparison operators in php
Screenshot3
<?php
 
$number1 = 10;
$number2 = 9;

if (false) {

echo 'inside.';

}

?>

output (Screenshot4) :-

comparison operators in php
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):-

Comparison operators in php
Screenshot5
<?php
$number1 = 10;
$number2 = 9;
if (1==1) {
echo 'inside.';
}
?>

Output (Screenshot6):-

comparison operators in php
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):-

Comparison Operators In php
Screenshot7
<?php
$number1 = 10;
$number2 = 9;
 
if (1==2) {
echo 'inside.';
}
?>

Output (Screenshot8):-

Comparison operators in php
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):-

comparison operators example
Screenshot9
<?php
$number1 = 10;
$number2 = 9;
if (1!=2) {
echo 'inside.';
}
?>

output (Screenshot10):-

comparison operators in php
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 in php
Comparison Operators Screenshot11
<?php
$number1 = 10;
$number2 = 9;
if (1!=1) {
echo 'inside.';
}
?>

output (Screenshot12):-

comparison operators
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):-

comparison operators
Screenshot13
<?php
$number1 = 10;
$number2 = 9;
 
if($number1>$number2) {
echo 'Yes.';
}else {
echo 'No.';
}
?>

Output Screenshot14

Comparison Operators In php
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):-

comparison operators in php
Screenshot15

<?php



$number1 = 10;

$number2 = 9;



if($number1<$number2) {

echo 'Yes.';

}else {

echo 'No.';

}

?>

Code for Example 2 (Screenshot16):-

comparison operators
Screenshot16

<?php



$number1 = 10;

$number2 = 9;



if($number1>=$number2) {

echo 'Yes.';

}else {

echo 'No.';

}

?>


Code for Example 3 (Screenshot17):-

php code
Screenshot17

Code for Example 4 (Screenshot18):-

code php tutorials
Screenshot18

Code for Example 5 (Screenshot19):-

php operators
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) :-

check password
Screenshot20

Output (Screenshot21):-

php tutorials
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) :-

password php
Screenshot22

output (Screenshot23):-

password incorrect
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..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates