PHP If else

In PHP If else is a control structure and it is the heart topic of PHP Scripts. It will be used whenever you have performed the conditional statements.

If

PHP if statement executes the block of code when the expression evaluates to TRUE

Syntax:

Example

Inside HTML / Javascript or CSS, we can write if statement like below:

We can write if statements within another if statements infinitely.

Syntax:

else

Whenever the expression in if statement evaluates to FALSE and it constructed with else statement and then the “else” block will be executed.

Syntax:

Example

Like if statement, else statement also can be nested infinitely.

else if

We can understand with the name itself. If we combine both else and if statements together, it is called as “elseif” or “else if” statement.
There should be else block after elseif block, if else part is missing after elseif block then PHP throws fatal error.

Syntax:

Consider the following example, there we have used all the if, else and else if concepts together.

Example

We will discuss later about the rand() function.

Click the below button to see the demo.
Demo

Please follow and like us:
error

Leave a Reply

Your email address will not be published. Required fields are marked *