Understanding If, Else, and Elseif Statement in PHP

php if statement: man holding a tablet with brain hologram

*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.

PHP If statement is one of the things you need to understand if you?re an aspiring programmer, web developer or coder. ?You need to learn how to code.

Sure, drag-and-drop websites are a thing, and WordPress, Squarespace, and other website platforms offer easy-to-use templates for the beginner and intermediate web designer, but if you hope to make it big as a website developer or computer programmer, you need to have what it takes to build truly unique, functional and user-friendly programs. This means you need to know how to code.

That said, everyone needs to start somewhere. After all, children aren?t born knowing what 2+2 is. All sixth graders are likely to assume that X is a party crasher to the algebraic equation. While a coding school can help you learn all you need to know about today?s codes, there are some basics that it wouldn?t hurt to brush up on. Some such basics include the PHP If, Else and Elseif statements.

The Purpose of the PHP If Statement

PHP if statement, else and elseif statements are actually extremely practical, and they allow a program to function with the same rationale as a human brain. If-else statements come in to follow a set of instructions if certain conditions are present. The best way to explain this is to use a real-life example. For instance, say that you want to make a decision about whether or not to bring an umbrella along on your walk. You look outside and see that the sky is overcast. You check the radar and decide that if there is rain in the forecast, you will bring an umbrella along. There is, so you pack an umbrella into your bag. You automatically make the decision to bring an umbrella based on the conditions that are present.

In any programming language, these conditions and the subsequent decisions should be complete before the launch of a program. PHP If statement, with else and elseif statements, can help a program determine how to react based on the presence of certain conditions.

In other words, if-else statements allow you to create conditions in the form of expressions that evaluate to either ?true? or ?false? base on the actions prior to arriving at a certain condition. In PHP, there are several statements that you can use to make decisions. Those include the following:

  • The php?if statement has a code if a condition is be true.
  • The if?else statement executes one block of code depending on if a condition is true and executes a different section of code in the case of a false condition.
  • The if?elseif?else statement executes variant sections of code as determined by more than one set condition.
  • The switch?case statement selects one of several blocks of code that can be executed depending on which conditions are met.

This guide explores each of these statements and helps you determine how to effectively use each. It also briefly explains what every novice programmer should know in order to succeed in the development world.

PHP If Statement

The condition is mandatory in order for a certain process to complete. It is within parentheses and denotes as the ?if statement.? If the conditions are OK, then the instructions inside the body are complete. If the conditions are false, then the statement is skipped and the program moves onto the next code.

A real-life example of this would be the umbrella example: if there is rain in the forecast, then I will bring an umbrella. If rain is not in the forecast, then I will not execute the decision to bring an umbrella.

If?Else Statement

Smart program design allows for enhanced decision-making by providing an alternative choice with an if?else statement. This statement allows your site to execute one block of code if the specified condition comes out to be true and another block of code if it evaluates to be false.

For instance, say that you want your program to tell visitors to “Have a Nice Day,” unless it is Friday, in which case you would want it to say, “Have a Nice Weekend!” You can program it to change which phrase to use by setting the if statement for every other day of the week, and the else statement for Friday. If the calendar says Friday, your program will execute the code to say, “Have a Nice Weekend!”

If?Elseif?Else Statement

The if?elseif?else statement exists if you want to combine multiple conditions. You can get as complex with these statements as you would like and have your program carry out all sorts of different codes based on certain conditions.

For example, imagine that you want your website to greet visitors with a salutation. However, you want that salutation to be consistent with the time of day. Your if?elseif?else statements can allow certain codes in the following manner:

  • If condition one is true, then it’s?Code A
  • Code B if condition one is false but condition two is true
  • If both condition one and two are false, then it’s?Code C

For instance, say that you set your site to read “Good Morning!” if the time is less than 1100. “Good Afternoon!” if the time is less than 1900. If neither of those conditions are met, your site will automatically generate a salutation of “Good Evening!”

Switch?Case Statement

Whereas the if?elseif?else statement is a set of multiple if?else statements, the switch?case statement is a series of if statements. A switch statement is used to check a variable against several different values. Switch statements can have multiple cases assigned to the same code block, meaning that separate conditions can do the same thing. You can also set a default code to a switch statement, just in case none of the conditions are met.

Other Coding Essentials

Coding involves much more than knowing the difference between PHP if statement ?else and switch?case statements. There are thousands of functions that programmers know, many much more complex than others. Additionally, programming languages are always changing, evolving and going out of style. As an aspiring coder, you need to be willing and able to research new functions on an almost daily basis, retain mass amounts of information and be willing to diversify. That said, there are certain terms that you should know that are nearly universal across most or all languages:

  • Constants: As its name implies, these are values that do not change throughout the application of a program.
  • Variables: These are identifiers connected to values that change throughout the execution of a program.
  • Data Types: These refer to a specific kind of data item that is defined by the values it can accept.
  • Data Structures: A data structure is a type of format used for organizing and storing data in a way that best suits your needs of the data.
  • Objects: Objects refer to the combination of related constants, variables, data structures and functions.
  • Conditionals: Conditionals refer to the way in which a code must choose to proceed based on given circumstances (this is where a PHP if statement would come in).
  • API: API is short for application programming interface and refers to a set of instructions and standards for accessing the functionality of a software application, operating system or utility.
  • Expressions: Expressions refers to mathematical statements that consist of numbers, variables and operations, and that resolve into specific values.
  • Loops: Loops are a sequence of instructions that are continually repeated until a certain condition is reached.

Programming Is a Way of Life

Programming uses its own unique jargon, and as when learning any language, you need to integrate it into every aspect of your life if you hope to master it. This is precisely what the best coders have done.

When you discover a new way of expressing yourself, you will view the world through a different lens. Programming offers you a new and exciting way to communicate and connect not just with the other people around you but with the world overall.

As you embrace this new opportunity, you may discover that the way in which you communicate isn?t the only thing that changes. You may find that your processes for doing the simplest of tasks, such as cleaning your apartment or getting dressed in the morning, change as well. Don?t be resistant to this change, as chances are that things will change for the better. For instance, you may discover that the way in which you clean your home is not nearly as efficient as the way that your brain wants you to clean it. Listen to your brain ? it won?t steer you wrong.

From memorizing thousands of terms and functions to changing your lifestyle, there is a lot that goes into coding. That said, you can avoid feeling overwhelmed by starting off easy, such as with the PHP if statement and other related statements. Have fun with these conditional statements and, when you?re ready, move onto bigger fish. Take it one day at a time and you?ll be programming in no time.

Recent Posts