

#Php if else code#
In the event that all if conditions evaluate to false, it executes the code provided in the last else statement.

#Php if else series#
30 & $age 40 & $age Īs you can see in the above example, we have multiple conditions, so we've used a series of elseif statements. code is executed if the expression1, expression2 and expression3 evaluates to FALSE, a default choiceĪgain, let's try to understand it using a real-world example. code is executed if the expression3 evaluates to TRUE code is executed if the expression2 evaluates to TRUE code is executed if the expression1 evaluates to TRUE Let's study the basic structure of the elseif statement, as shown in the following pseudo-code. If you've got more than two choices to choose from, you can use the elseif statement. We can consider the elseif statement as an extension to the if-else construct. So when you have two choices, and one of them must be executed, you can use the if-else construct. Let's revise the previous example to understand how it works. code is executed if the expression evaluates to FALSE code is executed if the expression evaluates to TRUE Basically, you can define it as shown in the following pseudo-code. You always use the else statement in conjunction with an if statement. That's where the else statement comes into the picture. More often than not, you also want to execute a different code snippet if the expression evaluates to false. On the other hand, if the expression evaluates to false, it won't do anything. In the previous section, we discussed the if construct, which allows you to execute a piece of code if the expression evaluates to true. On the other hand, if you have more than one statement to execute, you must use brackets, as shown in the following snippet. In fact, if you want to execute only a single statement, the above example can be rewritten without brackets, as shown in the following snippet. The above example should output the Your age is greater than 30! message since the expression evaluates to true. Let's have a look at the following example to understand how it actually works. The if construct allows you to execute a piece of code if the expression provided along with it evaluates to true. In this section, we'll go through a couple of important control structures that you'll end up using frequently in your day-to-day application development. In the previous section, we learned the basics of control structures in PHP and their usefulness in application development. Let's take a look at a few of these control structures with examples. PHP supports a number of different control structures: This is a crucial ability of the PHP language. In this case, a control structure ends code execution by redirecting users to a different page. Based on the user's login status, they will be redirected to either the Login page or the My Account page. In the above example, the program checks whether or not the user is logged in. The important thing to note here is that code execution continues normally after conditional code execution. If the condition is true, the conditional code will be executed. The following flowchart explains how a control structure works in PHP.Īs you can see in the above diagram, first a condition is checked. This could allow your script to give different responses based on user input, file contents, or some other data.


Generally, a program is executed sequentially, line by line, and a control structure allows you to alter that flow, usually depending on certain conditions.Ĭontrol structures are core features of the PHP language that allow your script to respond differently to different inputs or situations. In simple terms, a control structure allows you to control the flow of code execution in your application.
#Php if else how to#
I'll show you how to use all the main control structures that are supported in PHP, like if, else, for, foreach, while, and more. Today, we're going to discuss control structures and loops in PHP.
