PHP-and-MySQL/C2/Loops-Foreach-Statement/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
0:0 Welcome to the FOREACH loop tutorial.
0:02 This is the last loop I'm going to cover.
0:04 The basic fundamental of this loop is that it will loop through the values of an array
0:10 or the elements of an array
0:13 I remember that in my earlier tutorials I said that the elements of an array are also called id tags
0:21 The elements of an array aren't called id tags
0:24 When you're echoing out an array value
0:29 these here are the id - like numerical id, keys or tags
0:35 So I apologize for that
0:37 However lets get back to our FOREACH loop. Now we'll create an array to start with
0:42 I'm going to call this numbers and its an array. We now have to create these
0:47 I've shown you this in my earlier tutorials and we'll have the numbers 1 2 3 4 5 6 7 8 9 and 10
0:58 Ok. So, a FOREACH is like this
1:03 So we have FOREACH and then we have our condition here . Well I don't know what to call it
1:13 So, let me say the name of the array i.e. numbers
1:21 And then we say as and then we say value. So we can give this any name.
1:27 We could call it anything but I will type value
1:32 And then inside the curly brackets, the fundamental command would be echo value
1:40 And we'll concatenate a line break in the end and lets have a look at this
1:46 So that echoed through our loop. Its a really easy way to echo through our loop. You can use other loops to echo through an array. You have to write it manually, however and this is probably the easiest way to do it.
2:00 So as long as you remember this, you can echo through your array, perform operations on each part of your array and then maybe store it in a new array
2:07 However I'm going to show you how to manipulate in a simple way
2:12 Now what I'll do is - I'm going to do the 2 times table.
2:17 So I'll scrap this and I'll say the following.
2:23 So I need the number in the array here times 2 is and then outside of this is going to be the new value. So we're going to times each element of the array - each number in the array by 2
2:39 Let's start out by saying numbers
2:44 Sorry, we're going to say value because we have stored each FOREACH element in this variable name value
2:54 So value is each of these in turn through the loop
2:58 Therefore, value times 2 is, and then after this we'll put some brackets. Inside we'll type value times 2
3:08 Remember this is a mathematical operator - an arithmetical operator that I showed you earlier
3:13 It is a mathematical operator but the correct name is arithmetic
3:18 OK. This is going to multiply by two
3:22 Now to make this interesting what I'll do is to make this as a multiple
3:28 as a new variable
3:30 and the multiple up here
3:33 is going to equal 2. So you can guess by now that I've basically replaced that
3:38 I can change this as I please
3:41 Lets load this and refresh
3:44 Oh! We forgot the break
3:46 So, lets just add that in the end here
3:49 Well as we can't read it
3:52 Sorry, 1 times 2 is 2
3:54 2 times 2 is 4 all the way up to 10 times 2 is 20
4:00 We know that these are all correct
4:02 We can change this, lets say we want the 10 times table
4:08 refresh, 1 times 2 is... Oh! nah, we forgot to change this 2 into multiple
4:18 Now it will echo our number
4:21 Refresh
4:22 So 1 times 10 is 10, 2 times 2 is, 2 times 10 is 20, 10 times 10 is a hundred
4:28 So as long as we change the value of the multiple - lets say the 12 times table
4:32 Our 2 values are going to change
4:36 There we are.
4:37 So from this FOREACH loop and array I've created a really basic, multiple program with which you can see the times table for any set of numbers you like
4:49 So that's the FOREACH loop. Thanks for watching.
4:52 This is Mad Madhur dubbing for the Spoken Tutorial Project.

Contributors and Content Editors

Chandrika