PHP if else, else if Statements with examples

There are few conditional statements in PHP that is been used for putting different conditions. All the statements are as follows:

1. PHP if statement

It execute that code only if the situation is true otherwise not.
if statement Syntax:

Example:

Result
PHP!

2. PHP if else statement

If true then execute the result, but in case condition false then output will be other coded thing.

PHP if else Syntax:

Example:

Result:
PHP!

3. PHP if…else if….else statement

Sometime first condition get false then to check or test another condition we use this statement.

PHP if…else if….else Syntax:

Example:

Result:

If Else Statement in JavaScript

Conditional Statements

The conditional statements are used to perform different actions for different decisions.

Just like any other languages, JavaScript has the following conditional statements:

  • if for specifying a block of code to be executed, when a specified condition becomes true
  • else to specify a block of code to be executed, when the condition is false
  • else if (nested if) for specifying a new condition to test, when the first condition becomes false

The if Statement

Use the if statement to specify a block of JavaScript code to be executed when a condition becomes true.

Syntax of if statement

Example

Make a “Good Morning” greeting if the hour is greater than 06:00:

The if else Statement

Use of else statement for specifying a block of code to be executed when the condition becomes false.

Syntax of if else statement

Example of if else

If the hour is less than 12, create a “Good Morning” greeting, otherwise “Good Afternoon”:

The else if Statement

Use else if statement to specify a new condition when the first condition is false.

Syntax

Example of else if

If time is less than 12:00, create a “Good morning” greeting, if not, but time is less than 18:00, create a “Good day” greeting, otherwise a “Good evening”: