Did you know FaceySpacey is the creator of Redux-First Router and Universal?
The Startup Incubator.
Your one stop social media shop
Resource Categories:
Technical
Non-Technical
Refer A Friend
Popular Articles
DEVELOPMENT TOOLS 1 - VERSION CONTROL (Mercurial)
CONTRACTING 2 - TOP 10 EASY SITE CREATOR TOOLS
Subscribe
- Technical
-
PHP
-
OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP OOP PHP 2 - OBJECT METHODS (AKA FUNCTIONS) OOP PHP 3 - INHERITANCE IN OOP PHP 1 - Introduction To Programming PHP 2 - DYNAMIC CODE PATHS & VARIABLES PHP 3 - FUNCTIONS PHP 4 - SCOPE PHP 5 - HOW TO LEARN PROGRAMMING PHP 6 - ARRAYS PHP 7 - LOOPS PHP 8 - CONCLUSION & MORE LEARNING TECHNIQUES
- PHP 7 - LOOPS
PHP 7 - LOOPS
Understanding loops is the next big hurdle in learning to program. The reason you use them and why they are so prevalent in programming can be a little obscure at first. Let's dive into a quick intro about loops, and a few examples will make all clear.
Firstly, keep in mind the list of values stored in the $anything array from the PHP 6 - ARRAY tutorial. We’ll start with some code:
foreach($anything as $k => $v) { echo 'the key is ' . $k . ' and the value is ' . $v . '<br>'; }
The result in the web browser will be:
the key is 0 and the value is 7
the key is 1 and the value is 4
the key is 2 and the value is 8
the key is 3 and the value is 9
the key is 4 and the value is 10
the key is some_name and the value is 5
Quick note: the periods around the variables above are how you separate text strings (“literal strings”) from variables, and the <br> string at the end is simply HTML that says put the text strings echoed out to the browser on new lines.
And most importantly, the language structure
foreach($anything as $k => $v) {...
simply says take the array and iterate through it, aka “walk through it,” aka loop through it, etc. And then simply in the block between the parentheses that follow, $k and $v will be the variables used to refer to the keys and values of the array. That’s it. That’s the most important thing you need to know about loops in PHP.
Another quite note: if your code went like this:
foreach($anything as $v) {...
then you have access to only the values in the parentheses. It’s an extremely common shorthand, used when you don’t need the values of the keys, which usually is the case when the keys are incrementing numbers from 0 up.
The next thing to understand is the for loop. In languages like C, there is no concept of a foreach loop. All there is a “for” loop, which is a lot less automated. It doesn’t have intelligence of the array it’s iterating through. In programming terms, they’d say foreach loops are “coupled” with the concept of arrays. That’s not the case with for loops.
for ($i = 1; $i <= 10; $i++) { echo $i . ‘<br>’; }
So what do you think that will do?
1
2
3
4
5
6
7
8
9
10
Take a second before I explain what’s going on to guess what it’s doing...It’s simply echoing out the value of the $i variable to the browser, which starts as 1, since $i=1 is the initial assignment of the $i variable as set before the first semicolon. It ends at 10, because the second statement before the second semicolon says to stop when $i reaches 10. And the 3rd statement says to increment by one after each loop. That’s it.
Now let’s imagine iterating through our previous array using a for loop:
for ($i = 0; $i <= 4; $i++) { echo $anything[$i]’; }
That will simply alter the keys of the $anything array before echoing out the value for that key. It just so happens that the keys of the $anything array are ordered numbers from 0 to 4. If we tried to echo out the value of the $anything[5], nothing would be output since there is no value there. This loop would not be able to echo out the value of the ‘some_name’ key like the foreach loop would.
Related Entries
- OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP
- OOP PHP 2 - OBJECT METHODS (AKA FUNCTIONS)
- OOP PHP 3 - INHERITANCE IN OOP
- PHP 1 - Introduction To Programming
- PHP 2 - DYNAMIC CODE PATHS & VARIABLES
- PHP 3 - FUNCTIONS
- PHP 4 - SCOPE
- PHP 5 - HOW TO LEARN PROGRAMMING
- PHP 6 - ARRAYS
- PHP 7 - LOOPS
- PHP 8 - CONCLUSION & MORE LEARNING TECHNIQUES
SMM 3 - FORMULA TO FIND INFLUENCERS