PHP if else

Programming

PHP if else statement

PHP if else statement is used to test condition. There are different ways of using it. Unlike other programming languages, PHP also allows you to write code based on the effects of logical or comparative test conditions at runtime that perform specific actions. This means that you can create test conditions in the form of expressions that evaluate to either true or false and you can perform certain actions based on those results. When you write code, you will often want to perform different actions for different conditions. To do this, you can use conditional declarations in your code.

In PHP, there are many statements you can use for making decisions:

if statement – If one condition is valid, execute any code
if…else statement – Runs some code if a condition is true and a separate code if the condition is false
if…elseif…else statement – Carries out codes for more than two conditions
switch...case statement – Chooses one of several blocks of code to execute

PHP if else

if Statement of the PHP :
The if statement shall only be used to execute a block of code if the condition stated is valid.

Syntax :

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

if…else Statement of the PHP :
By adding an else statement to the if statement you will improve the decision-making process by giving an alternative option. The statement if…else allows you to execute one block of code if you evaluate the stated condition to true, and another block of code if it is evaluated to false.

Syntax :

if(condition){
// condition is true code will be executed
} else{
// condition is false code will be executed
}

if…elseif…else Statement of the PHP :
The if…elseif…else statement executes codes varying for more than two conditions.

Syntax :

if(condition A){
// condition A is true code will be executed
} elseif(condition B){
// Code to be executed if the condition A is false and condition B is true
} else{
// Code to be executed if both condition A and condition B are false
}

switch…case Statement of the PHP :
The switch…case the statement represents an alternative to the if…elseif…else comment, which is doing almost the same. The switch...case statement checks a variable against a set of values until a match is found, and then executes the code block that corresponds to that match.

Syntax :

switch (x) {
case label_A:
code to be executed if x=label_A;
break;
case label_B:
code to be executed if x=label_B;
break;
case label_C:
code to be executed if x=label_C;
break;

default:
code to be executed if x is different from all labels;
}

First, we have a single expression n (most commonly a variable), which is evaluated once. Instead, the expression value is compared with the values in the structure for each case. If a match occurs, the code block associated with the case will be executed. Using break to stop automatically running the code into the next event. If no match is found, the default statement will be used.