PHP Logical Operators Previous
Next PHP If Else Statement
PHP The if Statement
The if statement is used to execute some code only if a specified condition is true.
Syntax
if(condition){
code to be executed if condition is true;
}
Here is the code of if statement.
Filename: php / if-statement.php
Code |
<?php
// PHP if statement
$x=10;
$y=20;
if($x < $y){
echo "$x is less than $y";
}
?>
|
Running Step.
Type Url - https://localhost/php/if-statement.php
Output |
10 is less than 20
|
Filename: php / if1.php
Code |
<?php
// PHP if statement
$x=9;
$y=10;
if($x <= $y){
echo "$x is less than equal to $y";
}
?>
|
Running Step.
Type Url - https://localhost/php/if1.php
Output |
10 is less than equal to 20
|
Filename: php / is-greater.php
Code |
<?php
// PHP if statement
$x=20;
$y=10;
if($x > $y){
echo "$x is greater than $y";
}
?>
|
Running Step.
Type Url - https://localhost/php/is-greater.php
Output |
20 is greater than 10
|
Filename: php / is-greater1.php
Code |
<?php
// PHP if statement
$x=11;
$y=10;
if($x >= $y){
echo "$x is greater than equal to $y";
}
?>
|
Running Step.
Type Url - https://localhost/php/is-greater1.php
Output |
11 is greater than equal to 10
|
Filename: php / is-equal.php
Code |
<?php
// PHP if statement
$x=10;
$y=10;
if($x = $y){
echo "$x is equal to $y";
}
?>
|
Running Step.
Type Url - https://localhost/php/is-equal.php
Filename: php / is-not-equal.php
Code |
<?php
// PHP if statement
$x=11;
$y=10;
if($x != $y){
echo "$x is not equal to $y";gt;
|
Running Step.
Type Url - https://localhost/php/is-not-equal.php
Output |
11 is not equal to 10
|
Tags: PHP