1. 28
    Display the contents of an array with a for loop
    3m 38s

Display the contents of an array with a for loop

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet

Different from a foreach loop, a for loop in PHP allows you to iterate through an array based on an index or another conditional value.

Mark Shust: [0:00] We've previously used a foreach loop to display the contents of an array, but we can also use a for loop. [0:08] This has a bit of a weird syntax and is a bit more complex than foreach, but gives you some extra flexibility, including a built-in counter and the ability to exit out of a loop based on that counter value or another condition.

[0:24] Let's go ahead and replace this call to var_dump with a new PHP block. We'll start our new for loop, and then open up a parenthesis.

[0:37] For loops accept three parameters -- a counter, a condition, and an incrementer. Our counter will be a regular PHP variable. For some reason, it's common practice to use an i variable for counters probably just because it's easier to type, and perhaps it stands for increment or index.

[0:58] We'll start this with a value set equal to . This is to account for arrays being zero-based indexes, meaning that the first key of an array always starts with . Our parameters will be separated with semicolons.

[1:15] The next parameter is a condition. We will set this equal to i < numPosts. This will execute for however many posts we have and then no more. Finally, we need to iterate this counter. We will set this iterator equal to i++. This will add a value to the i variable on every iteration.

[1:41] We can go ahead and use curly brackets, just like foreach loops. One thing I didn't previously mention when we talked about foreach loops is that we also have access to an alternate syntax. Just like if and and if, we can use an alternate syntax with colons. We can end a foreach loop with end foreach. Similarly, we can replace this bracket with a colon and end the statement with endfor.

[2:15] The syntax is a bit easier to read within template files whenever you are outputting data. It's very common to use single-line PHP statements with this syntax. Let's go ahead and migrate this over.

[2:30] Within this loop, we can now output variables from our $_POST array. Let's go ahead and create an <h3> tag. Within it, we will use a short echo tag to output the value of $_POSTs. Remember that we can always reference arrays directly by their index. We can reference this first value of the $_POST array with a [$i] variable. This will initially be set equal to , which is the first record of our array.

[3:05] We can also tack on a second set of brackets afterwards. This is how we can access properties of this array. We can just reference the string of title to grab the title of this first index array. We can do the same thing for content, but this will be with a <p> tag. When we save and refresh this page, we will see each record of this $_POST array being outputted.