1 00:00:00 --> 00:00:01 Welcome to the FOREACH loop tutorial. 2 00:00:02 --> 00:00:04 This is the last loop I'm going to cover. 3 00:00:05 --> 00:00:09 The basic fundamental of this loop is that it will loop through the values of an array 4 00:00:10 --> 00:00:12 or the elements of an array 5 00:00:13 --> 00:00:20 I remember that in my earlier tutorials I said that the elements of an array are also called id tags 6 00:00:21 --> 00:00:23 The elements of an array aren't called id tags 7 00:00:24 --> 00:00:28 When you're echoing out an array value 8 00:00:29 --> 00:00:34 these here are the id - like numerical id, keys or tags 9 00:00:35 --> 00:00:36 So I apologize for that 10 00:00:37 --> 00:00:42 However lets get back to our FOREACH loop. Now we'll create an array to start with 11 00:00:43 --> 00:00:48 I'm going to call this numbers and its an array. We now have to create these 12 00:00:49 --> 00:00:59 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 13 00:01:00 --> 00:01:02 Ok. So, a FOREACH is like this 14 00:01:03 --> 00:01:12 So we have FOREACH and then we have our condition here . Well I don't know what to call it 15 00:01:13 --> 00:01:20 So, let me say the name of the array i.e. numbers 16 00:01:21 --> 00:01:26 And then we say as and then we say value. So we can give this any name. 17 00:01:27 --> 00:01:31 We could call it anything but I will type value 18 00:01:32 --> 00:01:39 And then inside the curly brackets, the fundamental command would be echo value 19 00:01:40 --> 00:01:45 And we'll concatenate a line break in the end and lets have a look at this 20 00:01:46 --> 00:01:59 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. 21 00:02:00 --> 00:02:07 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 22 00:02:08 --> 00:02:11 However I'm going to show you how to manipulate in a simple way 23 00:02:12 --> 00:02:18 Now what I'll do is - I'm going to do the 2 times table. 24 00:02:19 --> 00:02:22 So I'll scrap this and I'll say the following. 25 00:02:23 --> 00:02:40 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 26 00:02:41 --> 00:02:45 Let's start out by saying numbers 27 00:02:46 --> 00:02:55 Sorry, we're going to say value because we have stored each FOREACH element in this variable name value 28 00:02:56 --> 00:02:59 So value is each of these in turn through the loop 29 00:03:00 --> 00:03:09 Therefore, value times 2 is, and then after this we'll put some brackets. Inside we'll type value times 2 30 00:03:10 --> 00:03:14 Remember this is a mathematical operator - an arithmetical operator that I showed you earlier 31 00:03:15 --> 00:03:19 It is a mathematical operator but the correct name is arithmetic 32 00:03:20 --> 00:03:23 OK. This is going to multiply by two 33 00:03:24 --> 00:03:29 Now to make this interesting what I'll do is to make this as a multiple 34 00:03:30 --> 00:03:31 as a new variable 35 00:03:32 --> 00:03:34 and the multiple up here 36 00:03:35 --> 00:03:40 is going to equal 2. So you can guess by now that I've basically replaced that 37 00:03:41 --> 00:03:42 I can change this as I please 38 00:03:43 --> 00:03:45 Lets load this and refresh 39 00:03:46 --> 00:03:47 Oh! We forgot the break 40 00:03:48 --> 00:03:50 So, lets just add that in the end here 41 00:03:51 --> 00:03:53 Well as we can't read it 42 00:03:54 --> 00:03:57 Sorry, 1 times 2 is 2 43 00:03:58 --> 00:04:02 2 times 2 is 4 all the way up to 10 times 2 is 20 44 00:04:03 --> 00:04:04 We know that these are all correct 45 00:04:05 --> 00:04:09 We can change this, lets say we want the 10 times table 46 00:04:10 --> 00:04:19 refresh, 1 times 2 is... Oh! nah, we forgot to change this 2 into multiple 47 00:04:20 --> 00:04:22 Now it will echo our number 48 00:04:23 --> 00:04:23 Refresh 49 00:04:24 --> 00:04:29 So 1 times 10 is 10, 2 times 2 is, 2 times 10 is 20, 10 times 10 is a hundred 50 00:04:30 --> 00:04:35 So as long as we change the value of the multiple - lets say the 12 times table 51 00:04:36 --> 00:04:38 Our 2 values are going to change 52 00:04:39 --> 00:04:40 There we are. 53 00:04:41 --> 00:04:50 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 54 00:04:51 --> 00:04:53 So that's the FOREACH loop. Thanks for watching. 55 00:04:54 --> 00:04:59 This is Madhur dubbing for the Spoken Tutorial Project.