Repeating code using Loops
Lab: PHP 101: Gentle Introduction to WordPress Programming
Video Runtime: 13:57
There are many times when you need to repeat code over and over again. The WordPress main loop is a big one that we can all relate to. The premise of it is this:
- The
while
controls the code block, meaning the code does not run unless the conditional expression istrue
. have_posts()
checks to see if there any more posts to process- The code runs when there are more posts to run, meaning
have_posts()
returnedtrue
. - Then it loops back around to the
while
again. Repeat.
Therefore, when using the while
, the loop will continue looping (repeating the code within its code block) until the conditional expression is false
. You can build more complex conditional expressions, as you need to run the loop.
PHP provides many more looping constructs including foreach
and for
. You use the foreach
to loop through the items within a collection, meaning an array, or an object. For an array, you can pull out each element and then use it within the code block.
Why do we repeat with loops?
There are a couple of reasons. But here are the primary ones.
The first reason is to save yourself from having to type the same code over and over again. Why would you do that? If the same lines of code need to run multiple times, it doesn’t make sense to type that many times. No, that’s redundant. It’s also more code for you to test and maintain. Yuck. Looping lets you write it one time and then repeatedly run it over and over again.
The second reason is dynamic information. When you write the code, you don’t know how many elements will be in the array or how many posts there are for a given web page. Right? For example, for the main loop, if you are on the Posts Page, the number of posts to be processed will vary. Using a loop, you can iterate and decide at runtime how many times to run the loop.
Challenge Yourself
-
If there are 6 posts to be displayed on the Blog page, how many times will the main WordPress loop run?
-
If an array has 12 elements in it, when using a `foreach`, how many times will its code block run?
<?php | |
// This post does not have a featured image, | |
// i.e. thumbnail. | |
$has_thumbnail = has_post_thumbnail(); | |
while ( $has_thumbnail ) { | |
// does the code block run | |
} |
-
For the above code, will the `while` loop run the code block?
References
Here is a link to PHP type casting for you.
Your functions are bloated. Put them on a diet. Think "skinny" and "as few lines as possible."
Episodes
Total Lab Runtime: 03:21:32
- 1 Lab Introductionfree 11:37
- 2 What is PHP? Why use it?free 17:49
- 3 Why and how does WordPress use PHP?free 07:33
- 4 Syntax Basicsfree 18:34
- 5 What's the deal with Variables?free 27:05
- 6 Break Up Code into Logical Partsfree 07:55
- 7 Subroutines - Behold the functionfree 14:11
- 8 Loading Files to Runfree 13:20
- 9 To Run or Not to Run - Making Decisionsfree 19:07
- 10 Sequencing - Yup, code runs in orderfree 06:16
- 11 Repeating code using Loopsfree 13:57
- 12 Building Strings with Dots and Variablesfree 15:10
- 13 What's the deal with scope?free 08:34
- 14 Naming Stufffree 06:18
- 15 Putting it All Togetherfree 08:33
- 16 Where do I go from here?free 05:33