# PHP Control Structures and Loops: if, else, for, foreach, while, and More

Today, we're going to discuss control structures and loops in PHP. 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.

## What Is a Control Structure?

In simple terms, a control structure allows you to control the flow of code execution in your application. Generally, a program is executed sequentially, line by line, and a control structure allows you to alter that flow, usually depending on certain conditions.

Control structures are core features of the PHP language that allow your script to respond differently to different inputs or situations. This could allow your script to give different responses based on user input, file contents, or some other data.

The following flowchart explains how a control structure works in PHP.

As you can see in the above diagram, first a condition is checked. If the condition is true, the conditional code will be executed. The important thing to note here is that code execution continues normally after conditional code execution.

Let's consider the following example.

In the above example, the program checks whether or not the user is logged in. Based on the user's login status, they will be redirected to either the Login page or the My Account page. In this case, a control structure ends code execution by redirecting users to a different page. This is a crucial ability of the PHP language.

PHP supports a number of different control structures:

• if
• else
• elseif
• switch
• while
• do-while
• for
• foreach
• and more

Let's take a look at a few of these control structures with examples.

## Learning PHP Control Structures

In the previous section, we learned the basics of control structures in PHP and their usefulness in application development. 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.

### PHP If Statement

The if construct allows you to execute a piece of code if the expression provided along with it evaluates to true.

Let's have a look at the following example to understand how it actually works.

The above example should output the Your age is greater than 30! message since the expression evaluates to true. 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.

On the other hand, if you have more than one statement to execute, you must use brackets, as shown in the following snippet.

### PHP Else Statement

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 the expression evaluates to false, it won't do anything. More often than not, you also want to execute a different code snippet if the expression evaluates to false. That's where the else statement comes into the picture.

You always use the else statement in conjunction with an if statement. Basically, you can define it as shown in the following pseudo-code.

Let's revise the previous example to understand how it works.

So when you have two choices, and one of them must be executed, you can use the if-else construct.

### PHP Else If Statement

We can consider the elseif statement as an extension to the if-else construct. If you've got more than two choices to choose from, you can use the elseif statement.

Let's study the basic structure of the elseif statement, as shown in the following pseudo-code.

Again, let's try to understand it using a real-world example.

As you can see in the above example, we have multiple conditions, so we've used a series of elseif statements. In the event that all if conditions evaluate to false, it executes the code provided in the last else statement.

### PHP Switch Statement

The switch statement is somewhat similar to the elseif statement which we've just discussed in the previous section. The only difference is the expression which is being checked.

In the case of the elseif statement, you have a set of different conditions, and an appropriate action will be executed based on a condition. On the other hand, if you want to compare a variable with different values, you can use the switch statement.

As usual, an example is the best way to understand the switch statement.

As you can see in the above example, we want to check the value of the $favourite_site variable, and based on the value of the $favourite_site variable, we want to print a message.

### For Each in PHP

The foreach loop is used to iterate over array variables. If you have an array variable, and you want to go through each element of that array, the foreach loop is the best choice.

Let's have a look at a couple of examples.

If you want to access array values, you can use the first version of the foreach loop, as shown in the above example. On the other hand, if you want to access both a key and a value, you can do it as shown in the \$employee example above.

## Breaking Out of the Loop

There are times when you might want to break out of a loop before it runs its course. This can be achieved easily using the break keyword. It will get you out of the current forforeachwhiledo-while, or switch structure.

You can also use break to get out of multiple nested loops by supplying a numeric argument. For example, using break 3 will break you out of 3 nested loops. However, you cannot pass a variable as the numeric argument if you are using a PHP version greater than or equal to 5.4.

Another keyword that can interrupt loops in PHP is continue. However, this only skips the rest of the current loop iteration instead of breaking out of the loop altogether. Just like break, you can also use a numerical value with continue to specify how many nested loops it should skip for the current iteration.

## Conclusion

In this article, we discussed different control structures and loops in PHP. They are an essential part of PHP—or any programming language for that matter.

### Learn PHP With a Free Online Course

If you want to learn PHP, check out our free online course on PHP fundamentals!

In this course, you'll learn the fundamentals of PHP programming. You'll start with the basics, learning how PHP works and writing simple PHP loops and functions. Then you'll build up to coding classes for simple object-oriented programming (OOP). Along the way, you'll learn all the most important skills for writing apps for the web: you'll get a chance to practice responding to GET and POST requests, parsing JSON, authenticating users, and using a MySQL database.