Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP if

PHP Conditional Statements

When writing code, you often need to perform different actions based on various conditions. Conditional statements allow you to accomplish this.

In PHP, the following conditional statements are available:

  • if statement: Executes code if a specific condition is true.
  • if…else statement: Executes one block of code if a condition is true and another block if the condition is false.
  • if…elseif…else statement: Executes different blocks of code for multiple conditions.
  • switch statement: Chooses one of several blocks of code to execute.

PHP – The if Statement

The if statement executes a block of code if a specific condition is true.

Syntax

if (condition) {
  // code to be executed if condition is true;
}

Example

Print “Have a good day!” if 5 is greater than 3.

if (5 > 3) {
 echo "Have a good day!";
}

We can also incorporate variables in the if statement:

Example

Print “Have a good day!” if $t is less than 20.

$t = 14;
if ($t < 20) {
 echo "Have a good day!";
}