Hướng dẫn multiple if statements php

For efficiency, you're best to use a switch statement, and then to catch those you have not found in your cases, you can use a default.

switch(in_category){ //Begin switch statement.
case '7': //Check if it equals 7
include(TEMPLATEPATH . '/post-experts.php'); //Include our PHP code
break; //End this current condition.
case '6': //Check if it equals 6
include(TEMPLATEPATH . '/post-radio.php'); //Include our PHP code
break; //End this current condition.
case '5': //Check if it equals 5
include(TEMPLATEPATH . '/post-lifestyle.php'); //Include our PHP code
break; //End this current condition.

default: //If none of the above cases are found, do this.
include(TEMPLATEPATH . '/singleorigional.php'); //Include our PHP code
break; //End this current condition.   
}

Edit: I decided to return to this at a later date, to better explain why this is better.

An if else combination implies an order. E.g.

if(thingy == "thing1"){
//Do one thing
}
elseif(thingy == "thing2"){
//Do another thing
} 
elseif(thingy == "thing3"){
//Do a different thing
}
else{
//Catch anything
}

With this, it means it will check the first condition, if thing == thing1, if not, check if it equals the next condition which is thing == thing2 and so on. If you're generally always expecting thing1, then this might be okay, because you're just catching a few other things. However, realistically it's inefficient to check all possible conditions before you reach the solution you need.

Instead, by writing the equivalent switch statement:

switch(thingy){
case "thing1":
//Do one thing
break;
case "thing2":
//Do another thing
break;
case "thing3":
//Do a different thing
break;
default:
//Catch anything
break; //Break is not needed if default is the final case.
}

What this does instead, is grab the answer first, e.g. thing == "thing3", and then it'll skip the other cases that are irrelevant, and instead only do what it needs to do. It does not use an order, instead it works a bit like pointing to the correct answer. So it doesn't matter if your actual answer is the first case, or the hundredth, it only does what is relevant.

So to summarise: If you use a switch, and your answered case is the hundredth case, it'll point to what to do after that switch(answer) is found, if you were to use ifelse and your answer was the 100th variation of the ifelse, it would have to iterate through 99 other pointless checks before doing what it needs to do.

Slide 1

Nội dung chính

  • For One Statement:-
  • For more than one Statement:-
  • Sample Program
  • Nested if Statement
  • Sample Program
  • if else and Nested if else Statement.
  • Sample Program
  • Sample Program
  • Nested if else Statement
  • Sample Program
  • What is nested if else statement?
  • What is nested IF explain with example?
  • What is difference between if and nested IF?
  • What is the difference between Elseif and else if in PHP?

Nội dung chính

  • For One Statement:-
  • For more than one Statement:-
  • Sample Program
  • Nested if Statement
  • Sample Program
  • if else and Nested if else Statement.
  • Sample Program
  • Sample Program
  • Nested if else Statement
  • Sample Program
  • What is nested if else statement?
  • What is nested IF explain with example?
  • What is difference between if and nested IF?
  • What is the difference between Elseif and else if in PHP?

Most trusted JOB oriented professional program

DevOps Certified Professional (DCP)

Take your first step into the world of DevOps with this course, which will help you to learn about the methodologies and tools used to develop, deploy, and operate high-quality software.

Slide 2

DevOps to DevSecOps – Learn the evolution

DevSecOps Certified Professional (DSOCP)

Learn to automate security into a fast-paced DevOps environment using various open-source tools and scripts.

Slide 2

Get certified in the new tech skill to rule the industry

Site Reliability Engineering (SRE) Certified Professional

A method of measuring and achieving reliability through engineering and operations work – developed by Google to manage services.

Slide 2

Master in DevOps Engineering (MDE)

Get enrolled for the most advanced and only course in the WORLD which can make you an expert and proficient Architect in DevOps, DevSecOps and Site Reliability Engineering (SRE) principles together.

Slide 2

Gain expertise and certified yourself

Azure DevOps Solutions Expert

Learn about the DevOps services available on Azure and how you can use them to make your workflow more efficient.

Slide 3

AWS Certified DevOps Professional

Learn about the DevOps services offered by AWS and how you can use them to make your workflow more efficient.

If statement is used to execute when a statement or a block of statement only if the condition is fulfilled or true.

Syntax

For One Statement:-

if(condition/expression)
statement;

For more than one Statement:-

if(condition/expression)
{
Blocks of statement;
}

OR

if(condition/statement):
Blocks of statements;
endif;

Sample Program

Output

Nested if Statement

We can make nesting statement by nesting the if statement. It means when we insert a second if statement inside an if statement, we call it nested if statement or nesting the if statement.

Sample Program

Output

To know more about If Statement and Nested if Statement Click here – If Statement and Nested if Statement

if else and Nested if else Statement.

A statement executes code if if_condition is true and executes another code if if_condition is false.

Syntax

if(condition/expression)
{
Statement1;
}
else echo “Statement2”;

Example – when Condition is True then, if_condition executes. See Below

Sample Program

Output

when Condition is False then, else_condition executes. See Below

Sample Program

Output

Nested if else Statement

we insert a second if_else statement inside an if_else statement, we call it nested _if_else statement.

syntax

if(condition/expression)
{
if(condition/expression)
{
Statement1;
}
else echo “Statement2”;
}
else echo “Statement2”;

Sample Program

In the above program, The first Condition is true, then it enters in 2nd condition to check and when the 2nd condition is true then it prints that statement and skips other else statements. See the Output below:

Click Here For Next Part – Difference between Single Equal, Double Equal and, Triple Equal in PHP

PHP Fundamental Tutorials with Basic Demo by Chentan in 2020 – Part-1

PHP Fundamental Tutorials with Basic Demo by Chentan in 2020 – Part-2

What is nested if else statement?

A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.

What is nested IF explain with example?

A nested if statement is an if-else statement with another if statement as the if body or the else body. Here's an example: if ( num > 0 ) // Outer if if ( num < 10 ) // Inner if System. out.

What is difference between if and nested IF?

The nested if is an if statement used within another if statement. When we use if else if then an if statement is used within the else part of another if in this way,'nested if is similar to an if else if statement.

What is the difference between Elseif and else if 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.