From the course: PHP for WordPress

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Loops

Loops

- [Instructor] Loops are a fundamental part of PHP and WordPress specifically. In fact, the important portion of WordPress code is actually called the loop. And we'll look at two types of loops: while loops and foreach loops. But first, let's look at what they are. An if statement will execute a piece of code once if a condition is true, but loops will continue to execute a piece of code until the condition becomes false. There are lots of applications for loops like enumerating, performing repetitive math, and listing a set of items. But in WordPress, loops are most often used to process arrays. Now, before we get into actual code examples, let's look at the structure of each. While loops will do something while a condition is true. And these are general purpose loops used for math, enumeration, or anything that you can think of. Foreach loops are loops that do something for each item in a list and these are used to process arrays. The syntax for a while loop is this: while…

Contents