What is Conditional Statement.
Before you can make a decision, there must be a condition or conditions you need to decide from. Conditional statement allow you to make decisions based on the values of variables or the result of comparisons. Conditional statement are fundamental programming constructs that allow you to control the flow of your program based on a certain condition.
Note you can make use of just the opening PHP tag alone if your webpage contains nothing but just only PHP codes without HTML code, a good example of such webpage is the logout page of an application.
Types of Condition
- if statement
- if else statement
- if else if statement
If statement
The if statement allows
you to execute a block of code if a certain condition is true. Here's the basic
syntax:
if (condition){
//block of code
}
Explanation
The condition can be any
expression that evaluates to a Boolean value (True or False). If the condition
is True, the code block indented below the if statement will be executed. If
the condition is False, the code block will be skipped.
Example 1: Write a PHP script that will check if first number is greater than the second number
<?php
$number1=200
$number2=50
if($number1 > $number2){
echo "$number 1 is greater than $number2";
}