PHP if else statement is used to test condition. There are various ways to use if statement in PHP. Show
PHP If StatementPHP if statement allows conditional execution of code. It is executed if condition is true. If statement is used to executes the block of code exist inside the if statement only if the specified condition is true. Syntax Flowchart Example Output: PHP If-else StatementPHP if-else statement is executed whether condition is true or false. If-else statement is slightly different from if statement. It executes one block of code if the specified condition is true and another block of code if the condition is false. Syntax Flowchart Example Output: PHP If-else-if StatementThe PHP if-else-if is a special statement used to combine multiple if?.else statements. So, we can check multiple conditions using this statement. Syntax Flowchart Example Output: PHP nested if StatementThe nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true. Syntax Flowchart Example Output: PHP Switch Example Output: 34 is smaller than 56 and 45 In this tutorial you'll learn how to write decision-making code using if...else...elseif statements in PHP. PHP Conditional StatementsLike most programming languages, PHP also allows you to write code that perform different actions based on the results of a logical or comparative test conditions at run time. This means, you can create test conditions in the form of expressions that evaluates to either There are several statements in PHP that you can use to make decisions:
We will explore each of these statements in the coming sections. The if StatementThe if statement is used to execute a block of code only if the specified condition evaluates to true. This is the simplest PHP's conditional statements and can be written like: if(condition){ The following example will output "Have a nice weekend!" if the current day is Friday:
The if...else StatementYou can enhance the decision making process by providing an alternative choice through adding an else statement to the if statement. The if...else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false. It can be written, like this: The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!"
The if...elseif...else StatementThe if...elseif...else a special statement that is used to combine multiple if...else statements. if(condition1){ The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday, otherwise it will output "Have a nice day!"
You will learn about PHP switch-case statement in the next chapter. The Ternary OperatorThe ternary operator provides a shorthand way of writing the if...else statements. The ternary operator is represented by the question mark ( To understand how this operator works, consider the following examples:
Using the ternary operator the same code could be written in a more compact way:
The ternary operator in the example above selects the value on the left of the colon (i.e. 'Child') if the condition evaluates to true (i.e. if Tip: Code written using the ternary operator can be hard to read. However, it provides a great way to write compact if-else statements. The Null Coalescing Operator PHP 7PHP 7 introduces a new null coalescing operator ( To uderstand this in a better way consider the following line of code. It fetches the value of
Using the null coalescing operator the same code could be written as:
As you can see the later syntax is more compact and easy to write. What is else if statement?The if/else if statement allows you to create a chain of if statements. The if statements are evaluated in order until one of the if expressions is true or the end of the if/else if chain is reached. If the end of the if/else if chain is reached without a true expression, no code blocks are executed.
What are the 4 PHP condition statements?PHP Conditional Statements. The if statement.. The if...else statement.. The if... elseif....else statement.. The switch... case statement.. What is nested if...else statement in PHP?The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.
Is there an Elseif in PHP?In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.
|