Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to PHP
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Introduction to Conditional Loops

  • A while loop is a conditional control structure that executes a statement group repeatedly as long as its specified test condition remains TRUE .
  • A while loop’s test condition’s value is compared to TRUE before each execution of the loop’s statement group.
  • 'Looping over arrays' is a common programming function, and PHP provides several built-in functions for doing so. They work on the basis of an array cursor , which is a 'marker' for the 'current' array element:
    • current() – returns the value of the array element at the current array cursor position
    • key() – returns the key of the array element at the current array cursor position
    • next() – advances the array cursor by one
    • prev() – moves the array cursor back by one
    • reset() – sets the array cursor to the 1st element
    • end() – sets the array cursor to the last element
  • The list() construct and the each() function are also used to loop over arrays.
  • list() is used to assign values to multiple variables at a time from an indexed array.
  • each() returns key/value information for the current array element in an array and advances the array cursor by one. It returns FALSE if the end of the array is reached.
  • Unlike some programming languages, PHP does not have ‘block-level’ scope used with control structures.
  • Additional Resources:

Introduction to Conditional Loops

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Lesson Overview 0:08
    • Lesson Overview
  • while Loop 0:48
    • Definition
    • Syntax and Usage
    • Coding Example
  • Looping Over Arrays 13:16
    • Looping Over Arrays
    • Coding Example
  • Looping Over Arrays (cont.) 16:43
    • Internal Array Pointer
  • Array Traversal Functions 17:29
    • current ()
    • next () and prev ()
    • reset () and end ()
    • key ()
  • Indexed Arrays: Using current () and next () 20:40
    • Indexed Arrays: Using current () and next ()
  • Associative Arrays: Using key (), current (), and next () 24:06
    • Associative Arrays: Using key (), current (), and next ()
  • Array Traversal Functions (cont.) 29:41
    • list () Construct and Example
    • each () Construct and Example
  • Lopping Over Arrays Using list(), each() 42:13
    • Lopping Over Arrays Using list(), each()
  • Control Structure Scope 50:37
    • Definition and Example
    • Control Structure Scope Coding Example
  • Coding Conventions 54:20
    • Coding Conventions
  • Homework Challenge 54:58
    • Homework Challenge

Transcription: Introduction to Conditional Loops

Hello again, and welcome back to Educator.com's Introduction to PHP course.0000

In today's lesson, we are going to be introducing the topic of conditional loops.0004

Specifically, we are going to be introducing the most fundamental conditional looping structure, which is known as the while loop.0010

We are going to talk about looping over arrays, which is basically going over the elements in array and performing certain actions on them.0018

And this is an extremely common task that you will use throughout all of your PHP code.0026

We are going to talk about some functions used in looping over arrays, which are array traversal functions.0030

We are going to talk about the topic of control structure scope, which is another discussion of variable scope.0037

And we are going to go over some coding conventions as they apply to while loops.0045

A while loop is essentially a conditional control structure; it is kind of like an if statement or an else statement.0050

And what it is: it has a statement group, or a group of statements that get executed when a certain condition is met.0055

Unlike the if statement, though, or the switch statement, it allows the statements within its statement group to be executed repeatedly.0063

So, what happens is: for example, here is what a while loop looks like.0072

It has the while keyword; it looks very similar to an if statement; and it has a test condition.0077

And what it does is executes the statements within its statement group, if the test condition evaluates to true.0082

And that is the same type of test condition you can use for an if statement or an elseif statement.0090

The difference is that, unlike an if statement, once the statement group is executed, it doesn't move down to the next line of code.0094

It actually goes back to the top and reevaluates the test condition.0101

If the test condition is still true, it goes ahead and executes its statement group all over again.0106

And it does that repeatedly until the test condition equals true.0112

The key thing to note is that before it executes the statement group is when it evaluates the test condition.0116

And before re-entering that statement group, it reevaluates the test condition on each what is called "loop" of the structure.0122

While loops typically use what is known as a counter or a boolean flag that gets altered within the loop statement group0133

in order to end the execution of the loop.0141

I'll clean this up.0147

So, because this loop gets executed repeatedly until the test condition returns false, 0148

you need to have some way of making that test condition false.0156

And the way you do that is by altering some variable within your statement group, so that test condition will equal false, and that loop will stop.0159

Otherwise, you will get stuck in what is known as an infinite loop, which we are going to be talking about in another lesson.0172

Now, one thing to note, also, is that like an if statement, the statement group of a while control structure only gets executed while the test condition is true.0179

So, if we had a while statement where we set the test condition equal to the literal value false,0192

then, just as an if statement, if we had done the same thing, 0205

whatever statements are within that while statement group are never going to be executed.0208

Let's go take a look at a script called while.php; and the first thing we are going to introduce is what is known as a loop counter.0215

And a loop counter is something that you can use to know when to end the execution of the loop.0226

What we are going to do is create a loop that is going to output the numbers 1 through 5.0233

And what it is going to do is: each time the loop goes through, it is going to increment a counter variable by 1.0240

And then, the next time that loop goes through, it tests to see whether that counter variable is less than or equal to 5.0247

And if so, it continues to output until the incrementing of the variable makes the test condition false.0256

So, let's take a look at what that actually looks like: it's a little easier to see in action.0262

We are going to define a loop counter variable; and the most common loop counter variable that you will see0266

in all sorts of PHP scripts, and in all programs, actually, is called i.0272

And we are going to set that equal to 1.0279

So now, we have created a variable called i; it is equal to 1; and then, in our while statement, we are going to have a test condition0284

that says, "As long as i is less than or equal to 5, I want you to keep executing the statements within this while statement group."0290

And what we are going to do within our while statement group is just simply echo the counter, the variable i.0300

So (let me finish typing this), for example, when this loop first executes, i is going to be equal to 1.0307

It is going to go up to the while; it is going to check the test condition; is 1 less than or equal to 5? Yes.0317

So, it is going to enter the statement group; here, it is going to output i, whose current value is equal to 5.0322

And then, the key here is that the next step that we do is to increment the value;0327

and we are going to use the increment operator that we learned about in our lesson on operators.0331

What that is going to do is increment i, so that after this first while loop is executed for the first time,0337

at the end, i is going to be equal to 1; but then, when we increment it, it is equal to 2.0345

It is going to go back up to the top; it is going to say, "Is 2 less than or equal to 5?"0349

That is going to evaluate to true, so it is going to enter again; it is going to output 2.0352

It is going to then increment it to 3 and go up to the top and test "is 3 less than or equal to 5?"0357

Yes; it is going to output it.0362

It is going to do that each time until, once i is incremented to 5, it will say "Is i less than or equal to 5?--true."0364

It is going to output 5; and then, when we increment it, i is going to be equal to 6.0372

So, the next time that this while loop is entered, it is going to say, "Is 6 less than or equal to 5?"0377

It is going to return false, and then you are going to essentially jump out of the while loop.0381

So, let's go ahead and run this code and take a look at it in our browser.0387

And you can see, what it has done is output the five variables 1, 2, 3, 4, and 5.0396

Now, the other thing you can do, for example, is: let's say we wanted to output ten numbers--0400

you could just change the test condition to 10, so that it executes ten different times.0406

And then, we are going to get the output 1-10.0411

Now, the other thing that was mentioned was that you can also use what are known as boolean flags in a while statement.0418

Here, in this while statement, the while loop ends any time that i is less than or equal to 5; we return it back to its original value.0426

Well, you also can use a while statement to just test a boolean variable.0435

If the boolean variable is true, it goes ahead and evaluates the loop; if it's false, it doesn't.0440

So, what we are going to do is...before we do this, I want to introduce a function.0446

If we go to php.net, there is a function called rand that generates a random integer.0448

And what you can do is supply it with two arguments, a minimum integer and a maximum integer.0456

And when you call that function, it is going to return a random integer within that range specified by min and max.0463

So, what we are going to do is: in our while loop, each time the while loop runs, we are going to be generating a random number.0473

And we are going to be testing to see if the number is over 90.0480

And if the number is over 90, then we want to exit out of the loop.0484

And this is just a test example--something you can do with a while loop--and we are going to use a boolean flag to do that.0489

What we are going to do is create (actually, first I am going to comment this) a boolean flag.0497

And boolean flags, or boolean variables, are often started with the word is, because it's "Is it true? Is it false?"0505

And we are going to call it isOver90; the name implies, "Is 'over 90' set to true or false?"0513

We are going to set it to false initially; and then, in our while condition, we are going to say, 0520

"We want to continue to generate random numbers until we generate a number that is over 90."0528

The way we do that is: we use the NOT operator on isOver90.0533

So, what this is going to do is: every time this loop executes, when it comes back up to the top and evaluates the test condition,0540

it is going to perform the NOT operation on isOver90.0546

So, as long as isOver90 is still false, this test condition will evaluate to true, and it will continue to execute the loop.0551

So initially, because we set isOver90 to false, NOT false is going to equal true; the loop is going to be entered initially.0559

Now, within the while loop, we are going to generate a random number, just called randomNum.0567

And we are going to generate a random number in the range 1-100; and this is using the rand function.0573

And then, what we are going to do is output the random number that is generated in each execution of the while loop.0580

Now, this loop is going to continue, because isOver90 is initially set to false, indefinitely, 0598

until something acts to change this variable isOver90 to true, which will make the test condition evaluate to false.0606

So, what we can do is add an if statement that is going to test the value of randomNum on each execution of the loop.0612

And it is going to test of randomNum is greater than 90; then, we are going to change the value of isOver90 to true,0627

because now it is true; we have generated a random number that is over 90.0638

When this function eventually generates a random number over 90, (which it eventually will--that is the nature 0646

of a random number generator), isOver90 will be set to true.0651

And then, when it goes back up here to evaluate the test condition, it is going to say NOT true an evaluate to false.0656

It is going to jump out of the while loop.0660

And then, at the bottom here, after it exits the loop, we are going to output a message that says "a number greater than 90 was generated."0662

If we go ahead and run this script again (oops, it looks like I had a typo)...0672

I'm going to go ahead and comment out this section up here, as well, so we don't see that output.0689

I'll double-check things; it seems like we are having a problem here.0715

I'll make sure I saved the page; oh, there it is--it just took a while to run.0731

As you can see, when this boolean flag example ran, in this case, it ran through several numbers before it generated a number greater than 90.0738

As you can see, the last number generated is equal to 98, which is going to set that isOver90 flag equal to true.0749

And then, we are going to output that the number over 90 was generated.0755

Because of the nature of the random function, this loop is going to run a different amount of times each time the while loop is run.0758

So, if we refresh the page, we are going to get a different set of numbers.0766

In this case, it generated a number greater than 90 on the first call to the rand function.0769

If we do it one more time, we can see that it took 1, 2, 3, 4, 5 tries to get a number greater than 90.0774

This is an example of, instead of using a counter, using a boolean flag that you change the value of to true or false,0782

within your while loop, based on a condition that occurs.0789

One of the most common things that we are going to be using the while loop for, 0798

and loops in general (we are going to be learning about some more in future lessons), is to loop over, or iterate over, arrays.0801

Basically, what that does is: you have an array that has maybe 5 different elements in it, 0809

and you want to perform maybe some function on each element in the array (for example, output it).0813

Well, while loops allow you to loop continuously over an array, performing actions on each element.0818

And it is an extremely common programming cast.0824

One way to do this is to make use of a loop counter, which we had just talked about.0829

Let's go look at a file called loopingOverArrays.php.0834

Here, we declare an indexed array (at the beginning) that just contains the integers 1, 2, and 3.0839

And we are going to create a while loop that is going to loop over the array, and what it is going to do is output each element of the array.0846

So, what we can do is: we are going to declare a loop counter, and we're going to set it equal to 0 (and I'll explain the reason for that in a second).0851

And then, our test condition is: we are going to say, "As long as the loop counter is less than count($indexArr)..."0861

Now, the count function returns the number of elements in an array, and we know indexed arrays start with a 0 index.0870

So, if there are 5 elements in an array--or, here there are 3 elements in this array--count is going to return 3.0882

So, it is going to say, "As long as the counter is less than 3, I want you to go ahead and run the loop."0890

Since the counter is initially set to 0, it is going to execute for the value i=0, i=1, i=2.0895

When i equals 3, it is going to jump out of the loop.0902

So now, what we are going to do is: within the loop, we are going to output the particular element that we are going to access using the i counter.0909

For example, we are going to output (it looks like I have a typo here) indexArr, 0919

and using the square bracket notation with some double quotes, what we can do is: 0932

within the square brackets, for indexArr, we can include the variable i, which is our counter.0937

And what that is going to do is say, "OK, I want you to echo the value of indexArr that has the index i."0942

And because arrays are 0 indexed, and they start with 0, it is going to, on the first iteration of the loop, output the value with index 0,0951

which is going to be 1; the second iteration--it is going to output 2; and so forth.0959

And then, the key thing that we need to do is to increment the loop counter.0969

Otherwise, the loop will just continue to run indefinitely, because i is going to be set to 0, so it is just going to keep going over and over again.0973

So, if we go ahead and run the script, we can see that it output 1, 2, and 3; 0981

so, it has looped or iterated over the array and output each value of the array.0990

And the way we did that, in this case, was using a loop counter.0997

Now, there are other ways to loop over arrays in PHP; and PHP actually has a bunch of built-in functions that are useful1004

for working with and looping over arrays.1010

And the functions work on the basis of what is known as an internal array pointer, or an array cursor.1013

Essentially, that is a marker that says where in the array the cursor is located at.1020

For example, when you initially create an array, the internal array pointer (or the array cursor) points to the first element in the array.1026

Then, there are different functions that you can use to move the cursor.1034

For example, you could use the next function to move the cursor down one; 1037

so now, the current element pointed to by the internal array pointer is the second element.1040

Let's take a look at some of the functions that do this.1046

The first function we are going to talk about is called current; and what that does is returns the value of the array,1052

at whatever position the array cursor is currently located at.1060

So, when you first declare an array, if you were to run the current function on that array (and all of these functions take 1063

an array as their argument)--if we called current and passed it the variable sample (which down here, you can see, is an array1071

containing 5 integers), if we had just declared sample, the array pointer is going to point at the first element of the array;1081

so, when we run current, it is going to return the value at whatever element is pointed to by the array cursor.1088

So, it is going to return this first element.1095

Before I talk about key, I want to talk about these other ones.1097

There is also the next function and previous function, which advance the cursor or move it back by one.1101

When this array is first declared, we then ran next on the array.1108

Now, initially the array cursor pointed here; after we run next, the array cursor points to 2.1119

So, if we were to run current on this array (let's say we were to echo it), 1124

current is going to output the value of the element currently pointed to by the array cursor.1136

Since we have advanced it to 2, this is going to output 2.1142

I'll clean this up.1146

And the same functionality works for the previous array, as well, except maybe if we had run next a couple of times,1152

so the cursor had been advanced out here to 3, we could use the previous function to move the cursor backwards.1159

Two other functions that move the cursor around are the reset and end functions.1165

The reset function returns the cursor to the first element in the array.1171

And the end function, as the name implies, sets the cursor to the last element in the array.1175

Now, when working with associative arrays, we can make use of the key function.1181

which is an analog of the current function, in that (for associative arrays, they have keys) 1186

it will output whatever the key is for the current element that the array cursor points to.1192

And actually, it is not just for associative arrays; these indexed arrays have keys, as well.1199

For example, this index is 0; this is 1; this is 2.1205

When this array is first declared, if we echoed the result of the key function on sample,1209

and if we did it immediately after we declared this sample array, it is going to output the value 0,1225

because the key of the current element pointed to by the array cursor, which is the key of the first element in the array, is 0.1231

So, let's go and take a look at some of these functions for looping over arrays.1241

We have a file called loopingOverArrays2; and the first thing we are going to do is loop over an indexed array.1245

And here, we have created an indexed array, the same as before, with the numbers 1, 2, and 3 in it.1250

And what we are going to do is declare a loop counter, so we know how many times to loop over the different arrays.1258

We are going to loop over it as many times as there are elements in the array.1265

So, we are going to create a loop counter again and set it equal to 0.1268

And we are going to use the same test that we had before, which is:1273

we are going to test if the loop counter is less than the length of the array we are looping over.1279

Now, what we want to do is retrieve the current value on each iteration of the array.1287

So, when we initially start this array, the array cursor is going to be pointing at the first element in the array, which is going to be equal to 1.1294

So, when we run current on the first iteration of the loop (we run current here on indexArr), 1302

it is going to return the value where the current cursor position is, which is going to be 1.1309

Then, we are going to go ahead and output this.1315

What we have done is: when we enter this array, i is going to be equal to 0.1341

And what we are going to go ahead and do is: on the first iteration, we are going to get the value of the current element,1346

which is going to be this first element, 1.1352

So, we are going to have value, and then we are going to output "current value=" in this case "1."1354

Now, instead of incrementing a counter, because we are not accessing the elements in the array using a counter--1361

we are not doing what we had done before in this example, where we access the elements of the array to output them1371

using the counter as the index into the array--and we are going to make use of these new array traversal functions,1377

we are going to go ahead and increment, or advance, the array cursor by 1, using the next method.1384

What this is going to do: calling next on indexArr is going to advance the cursor by 1.1391

So, after this loop runs for the first time--after it outputs the first value--the cursor is going to be incremented by 1.1396

So now, the cursor is going to point to the second element in the array.1402

And the reason here that, even though we are not using the loop counter to access the array, we are including it,1407

is because it is going to be used to define how many times we loop over this particular array.1411

If we go ahead and look at the output of this script, we can see that output($currVal) equals 1, 2, and 3.1418

This made use of the current function and the next function for looping over indexed arrays.1428

I'll go ahead and comment out this section, and we are going to do the same thing for associative arrays.1438

Here, we have defined an associative array with three key/value pairs, 1447

the keys being strings 1, 2, and 3, and the values being the integers 1, 2, and 3.1454

And then, we are going to loop over the array as many times as there are elements in the array.1462

So, we are going to be declaring a loop counter again; we are going to be using the same test,1466

except we are going to be counting the number of values in the associative array.1470

We are going to be incrementing the loop counter at the bottom.1474

And at this point, what we are going to do in each loop is retrieve 1478

the value of the key and of the value of the value of the current element pointed to by the array cursor.1481

And the way you do that, to get the key, is use the key function (which we had talked about).1492

So, running a key on assocArr is going to return the key that is currently being pointed to by the array cursor.1497

Since we just defined this function previous to the loop, the array cursor is going to point 1505

at the first element, which is going to be this key/value pair right here.1511

So, key, in this case, on the first iteration, is going to return the value 1.1515

Then, just as before, we can use the current function to get the current value.1519

And for this first iteration of the loop, it is going to be the value equal to 1.1524

And then, we are going to output that; and this is just going to be a formatted output.1528

It is going to say that the associative array that has the key currKey has the value currVal.1551

Then, you can see: what we do as we go ahead as we did before is: we move the array cursor up one,1566

so that, when we go to the loop the next time through, it is going to access the key here and the value here of that next element.1571

And then, we are incrementing the loop counter by 1, so that this loop eventually ends when we reach the number of elements in the array.1582

We go ahead and save this and run the script again.1590

We can see that we have gone ahead and looped over this array called assocArr.1596

And then, the key 1 is associated with the value 1; 2 is associated with 2; 3 is associated with 3...as we expect.1600

And this is making use of the key, current, and next array traversal functions.1607

One thing to notice is: let's say we actually commented out this next statement within this loop; what is that going to do?1613

Well, we are always going to run through this loop (in this case) three times, because i is going to take on the values 0, 1, and 2.1622

Once it equals the value 3, it is going to kick out of the loop, and that is because we are incrementing it at the end of each loop.1630

However, because in this loop we never advance the array cursor by 1, and the cursor is initially set to this first element in the array,1636

which is the key/value pair "the string 1=1," this loop, each time it goes through, 1647

is going to just output three times that initial key/value pair.1653

The reason is because we never advanced the array cursor.1657

For example, if we comment it out and run it again, we are going to see that we get the same output three times.1659

That shows the importance of making sure that you use this next function to be able to advance the array cursor.1669

Another thing to note is: let's say we were to run the same exact loop all over again--run it twice.1677

So, it is the same thing, and now we are going to run it a second time; well, let's see what happens when we go ahead and output the script.1688

We are going to see that, the first time it goes through, it outputs 1, 2, and 3, as expected.1700

But the second time through, it doesn't output anything.1704

And what is the reason for that? Well, the reason is that after this first loop occurred, we had run the next function on assocArr three different times.1707

So, by the time this loop ends, the array cursor, which had started here, had advanced to one past the last element in the array.1720

So, when we go down here and try to retrieve the key and current value of the element that the array cursor is pointing to,1730

it is not going to point to anything, because the cursor has gone on beyond the end of the array, which is why nothing gets output.1739

Now, the way we could solve this is, as we learned about, the reset function.1748

We can reset the associative array, and what that is going to do is put the array cursor back to the first element in the array,1753

so when this runs the first time through, the array cursor is going to be pointing at this first element.1762

So now, we should just see our output doubled.1769

If we go ahead and refresh it, we can see that the same thing happened over again.1772

Now, there are two other functions that are used often, and more frequently, actually, than the last ones that we just went through,1783

to loop over or traverse or iterate over arrays.1789

One is not a function; it is a language construct called list; it sort of acts like a function.1794

And then, there is an actual function called each.1799

Now, before we talk about how to use these to traverse over arrays, I want to explain how they work.1804

The list construct is used in this way: let's say we have declared an array sample with three values in it.1809

Well, what we can do is use the list construct: we type list and then an opening parentheses, 1817

and then we can put in a comma-separated list of different variables.1823

And when we set this list construct equal to an array, what it does is populates 1829

each of these variables specified within the parentheses of list with the values of this array.1835

And it does so...the first value specified in this list construct only works on associative arrays,1842

so the first variable we specify to list is going to be set to whatever value is in the indexed array sample that has the index 0.1849

So, in this case, variable 0 is going to get set to a.1861

And then, the second variable listed is going to access the value in the sample array with the index 1.1865

So, b has that index; so 1 is going to get set to b, and then 2 is going to get set to c.1874

Now, if we go and look at listEach.php, which is a script, we have 2 arrays defined at the beginning: indexArr and assocArr.1885

This one just, in this case, contains 2, 4, and 6; and then, this one is the same as we saw in our last sample file.1898

So, what we have here is a list statement that has three variables, and we set the list construct equal to the indexed array.1904

So, as we just learned, what is going to happen is: 0 index is going to be set equal to 2; 1 index is going to be set equal to 4; 1913

and then, 2 index is going to be set equal to 6.1921

So then, if we go ahead and echo each of these different variables, we should see that they are set to the values 2, 4, and 6, as expected.1925

If we save this file, and we load it up, we can see that 0 index, 1 index, and 2 index are set equal to 2, 4, and 6.1934

Now, when using the list function, you don't have to specify as many variables as there are values in the array.1946

For example, let's say we just want to grab the first two values from this array, the one at the 0 index and the value at the 1 index.1956

When we run this function, only these two values are going to get set.1966

We run the script again; well, first of all, if we look at this output, it says 0 index, 1 index, 2 index...1972

we are going to see that it outputs 2 and 4, because 0 index and 1 index were able to be set.1980

However, because we never actually set the variable 2 index, and we are trying to echo it in our echo statement down here, we get that undefined variable error.1986

And one other thing to note is: let's say we just want the first and third variable in the array.1998

Well, you can do that with list by just not putting anything in for the second element.2005

For example, if we want the value of the array at the 0 index and the 2 index (which would be the first and third element),2011

and we don't want the value at the 1 index; in list, we can use a comma-separated list that just adds an extra comma2018

and leaves out any variable for the second value in the array.2029

In this case, 0 index is going to get set to 2; 4 is going to get set to nothing, because nothing was specified.2037

And then the variable 2 index is going to get set equal to 6.2043

So, if we don't try to get an error down here again...now, when we rerun the script, it still has one index in here, but2047

we can see that (I'll go ahead and correct that, so that it looks better) 2061

we successfully used list to just extract the first and third values of the indexed array.2068

So here, we can see: the 0 index is equal to 2; the 2 index is equal to 6.2075

Now the each function--to talk about that: what it does is: when you run the each function on an array, the parameter you pass it is an array.2081

It returns an array that contains information about the current key/value pair of the array.2092

So, wherever that array cursor is pointing to in the array; when you run this function, it is going to provide you back an array2098

that has information about the key and value of the current array element.2105

The other thing that it does (and you can see the & here) is that, after it does that, it advances the array cursor by 1.2110

So, after you have run this function, it is the same thing as if you extracted the key/value information from the current element,2117

using maybe the key and current functions, and then ran the next function on the array.2123

So, each sort of has the same functionality as that, built into one.2134

And one other thing to note is that, if each is at the end of an array (meaning that you run each on an array 2141

where the cursor is pointing to the last element), it is going to return false.2149

And we will see how that will be useful in a second.2153

So, when this each function runs on the sample array (let me erase some of this, so it's easier to see)--2155

the sample array contains the strings a, b, and c--it is going to return a value that, as mentioned, is an array.2162

And the array is going to have the form of an associative array, so each return is going to be an associative array that has several key/value pairs.2170

When this function here, each, runs, it returns an array, and it's an associative array that has 4 elements in it.2217

It has 2 elements that correspond to the value of the current element (in this case, the value associated with the index 1 2225

in the array that each returns is set to the value of the current element--in this case, 1).2240

You can also access it because it creates a key called value, and it sets that equal to the value of the current element.2246

And then, it creates two other elements in the array that it returns: one with the key 0, and one with the key "key."2252

And that returns the key of the current element--in this case, assuming we had run this immediately after sample was declared,2262

the value returned is a, because the cursor points to a.2268

And the key of an indexed array is the numerical index, and in this case, it's 0; so, as we can see, this is what this returns.2272

Let's go take a look at each in action.2280

What we are going to do is: we have our associative array up here that has the three key/value pairs in it: 1, 2, and 3.2292

And we are going to loop over that associative array, and what we are going to do is run each on that array every time this loop occurs.2301

Now, as we just learned, what that is going to do is: this function is going to generate an associative array 2319

that contains information about the key/value pair of the element that is currently being pointed to.2326

And it is also going to advance the array cursor by 1, so when we run each again, 2333

it is going to get the next element in the array, because the cursor is going to advance.2339

Then, what we are simply going to do is print out the each return, which is the array returned to this, so we can see what information it contains.2343

And then, we are just going to increment the loop counter by 1; and that is in order to make sure we loop over the entire array.2354

If we go and run this function again (let me comment out this first part), and we reload the page,2361

we can see that, on each iteration of the loop, we ran the print_r function 2380

on the eachRtn array, which was the array returned by the each function.2384

And as we can see, as we saw in the slide, it's an array that contains four elements, four key/value pairs.2389

The first key is the number 1, and that is the value of the current element.2399

It also has a key called value, which also echoes the current value of the current element.2404

And then, it has two keys: 0 and the key named "key"; and that outputs the current value of the current key.2409

And so, as you can see, each time this is gone through--the first time, the value output was 1, because that was the first value in the array.2418

And it output the key; it set the key equal to the variable 1, because if we look back at our array, the key is the string 1.2425

And then, the second time it went through, each return array contained the value 2 as the key and the value 2 as the variable.2437

And if we look, we can see that that happens here, for both 2 and for 3.2444

One thing to note is that...let's say we go ahead and run this loop one extra time, and we add a +1 here to the end.2449

That means that it is going to try to run the loop after it has processed all of the elements in the array.2458

Well, we learned that each returns false whenever it reaches past the end of a particular array--when the array cursor moves past the end of an array.2464

So, when we go ahead and print each return here, each return is no longer going to be an associative array; it's going to be the value false.2476

And as we will see when we print it out, it is going to show that false was actually output.2482

And we are going to have to test for that, because false, when converted to a string, is just the empty string.2487

So, if each return is true, we are going to echo the array, or print out the array.2496

If each return is false, we are just going to output the string "false."2506

When we run this one more time, we can see that, on that fourth iteration of the array, 2520

each returned the value false; and so, here we have output the value false.2527

Now that we have learned about how each and list work, we can use that to traverse or iterate over an array.2537

If we go to this new file called listEach2.php, first we are going to loop over an indexed array.2544

Here we have created an indexed array with the values 2, 4, and 6.2550

Because we know the importance of...if we want to loop over the whole array, we want to make sure that the array cursor points2554

to the beginning...we run reset on the array originally, even though we just defined it; it is a good practice to do.2559

We are going to define a variable to hold the current array index; and we are going to explain that in a second.2566

What we can do is: because each returns the value false when it reaches the end of the array,2577

we can use that as part of our while test condition, because it evaluates to a boolean variable.2582

What we are going to do is use the list construct in combination with the each function.2589

So, if we type list...if we type this statement here, now what is going to happen is: when this while statement first gets executed,2596

it is going to test the test condition, and the first thing it is going to do is run this assignment operation.2616

And that is going to say, "Run the function each on indexArr," and it is going to, 2624

as we know, return an array, assuming that the cursor is pointing at a valid array element.2629

And then, it is going to set (as we know from the list function) this first element of list equal to whatever has the index 0 in this returned array.2636

And in this case, it's an indexed array, so it is just going to return either 0, 1, or 2.2649

And then, in the second part, we have list set to val; and what that is going to do is: because this is the setInt argument2658

to the list construct, it is going to take on the value of whatever the indexed array returned by each has at index 1.2667

In this case, key we are just going to leave blank, which you will commonly see, because in the indexed array, we are just concerned with the val.2677

And val is going to get set to whatever the value is at the current cursor position.2685

What we can do is output, on each run of the loop, the value that it pulls from the array.2692

Actually, we don't...let's ignore this array index...2706

What is going to happen is: each time this loop runs, it is going to pull the current key/value information from the indexed array.2714

It is going to advance the array cursor by 1; it is going to set value equal to the value in the array returned by each that has the index 1...2723

which, as we learned from our previous example, the index 1 refers to the current value, and then the index 0 refers to the key.2736

Because we are not concerned with the key, we can just leave this part blank.2748

And normally, this would be filled with whatever value is associated with the 0 index of the array on the right side of the equal sign of the list operation.2752

So, when we run this, what it is going to do is loop over indexArr and output each value.2762

Oops, that is a different script.2772

And so, you can see, what it does is: when it looped over the indexed array, it output the values 2, 4, and 6,2781

which, as you can see, were the three values in this array.2786

Now, where you will more commonly see this list(), each() combination is in looping over associative arrays.2789

Let me comment out this section up here.2797

And here, we have declared an associative array with three elements in it--three key/value pairs.2804

We reset the cursor to the beginning of the array; and now, we are going to use the list construct, 2810

along with the each function, to get both the key and the value from the current array cursor position.2816

When we run each on assocArr, every time this test condition is evaluated, it is going to return an associative array.2827

that has four elements in it: one of them has the index 0; one has the index 1.2836

The one with the index 0 is the key of the current element in the array, and the one with the index 1 is the value of the current element in the array.2842

Because we know list, when you specify two variables in the list construct, the first value gets set,2850

and the first variable gets set to the value of the array that is at the 0 index on the array specified to the right of the equal sign,2859

and the second element gets set to whatever value is in this array at the 1 index,2870

key and value are both going to get set to the output of each.2876

Additionally, the array cursor is going to get incremented by 1, so when we reach the end of the array, each is going to return false.2878

The value of this assignment statement is going to be false, and the while loop is going to end.2884

What we can do is output each key/value pair; so let's see, I'll call it...2889

And all this echo statement does is: it is going to output the string assocArray that is going to represent our variable.2914

It is going to include in the square brackets the key that we pull from the associative array.2921

And then, it is going to output an equal sign with the value that we pull from the array.2925

If we go ahead and run this script again (reports error...it looks like I added an extra parentheses in here),2931

you can see, what it did is looped over the associative array.2950

It output the key/value pair for each element in the array; the key 1 has the value 1, the key 2 has the value 2, and so on.2953

Now, one thing that we could do to also show you how these different array traversal functions work:2968

Let's say, instead of calling reset at the beginning of this loop, we called end.2976

So, the array cursor is going to actually point down here at the last value in array, which is going to be 3.2980

Well, when we go to this while statement, when we run this each function, it is not going to return false; 2987

it is going to return the key/value information associated with the last element in this array.2993

But then, when it goes through a second time, because the cursor is advanced past the end of the array,2998

each is going to return false, and so the loop is going to exit.3004

So in this case, only the third element is going to get output; so if we refresh the page, we can see that assocArr equals 3.3007

Another thing you can see is: let's say we, instead of specifying end here, specified next, 3016

which is going to initially set the array cursor at the second element in the array.3021

Then, this loop is going to actually only output the last two elements.3029

If we refresh, we see that it outputs 2 and 3.3033

One thing I want to talk about is control structure scope, or what some languages call block-level scope.3039

And basically, what I am going to be talking about is that PHP doesn't have that.3045

In PHP, there is only global scope and local function scope, which we have talked about.3048

In some languages (and the reason I am explaining this is because some of you might have experience with other programming languages),3053

if you declare a variable...let's say this is an entire PHP script: this variable, ifVar, is first declared within this if control structure.3058

Well, in many programming languages, if a variable is declared within a control structure, it is not available outside the control structure.3070

So, for example, if we declared ifVar equals 1 within this if statement, if we tried to echo it out here, it would say the variable was undefined.3079

That is, in other programming languages...PHP doesn't have that...what is known as control structure, or block-level, scope.3088

If you declare a variable within a control structure, it is in the global scope.3095

Because this is true, we are always going to execute this if statement; ifVar is always going to get set equal to 1.3100

And then, because ifVar doesn't have scope restricted to the control structure--it has global scope--3106

this is going to output 1, because it is going to know the value of ifVar, because it was declared in here.3113

If we go and actually look at the file called scope.php, where we have the same code, and we go ahead and run it,3121

we can see that it outputs the value 1.3134

One thing to note is that...let's say this was false, so ifVar never got set to the value equal to 1.3136

Well now, what is going to happen is: when we try to access ifVar down here, it is going to say that the variable hasn't been declared,3146

because it never actually reached this declaration statement, and it wasn't declared prior to this if statement in the file.3151

If we go ahead and run this file, we are going to say "undefined variable ifVar."3159

So, even though you can declare variables within control structures and access them outside of the control structures,3164

it's a common, and I consider, a good practice to go ahead and always declare your variables outside of the control structure,3172

and at least set them to some default value (in this case, we will set them to the empty string).3181

Now, what that is going to do is: then, when we try to access ifVar down here, if it doesn't get set in this if statement,3185

which it is not going to, because the test condition is false, it is just going to output the empty string, and we are not going to get the error.3190

Now, the other reason for doing that is that, if you are working on a team of people, 3196

everybody may not be familiar with how PHP works, and that they don't have local control structure scope.3202

And so, in those cases, those people are going to look at this and say, "Well, if this statement is missing right here, ifVar is not going to work."3207

So, by including the declaration outside of the control structure, those people are going to understand what is going on,3219

because otherwise, when they initially look at this, they are going to say, "Hey, wait a minute; this doesn't work."3233

So, it is also something to just increase readability among different people on your team.3237

So again, just to review that: PHP does not have any block-level or control structure scope.3245

Anything declared within a control structure, if it is declared in a global script file, is in the global scope, and is accessible outside of the control structure.3250

The coding conventions for while loops are going to look like this.3261

It is going to be basically the same as an if statement, except we have a while keyword instead of if.3267

We have a space in here between the opening parentheses of the test condition and the keyword.3272

There is a space here between the opening curly brace and the ending parentheses.3277

We have the statement group that is indented one indent further than the while statement.3282

And then, we have the last closing curly brace on its own line at the same indent level, here, as the while keyword; so these are on the same indent level.3288

For the homework challenge today, I want you to create an indexed array called lower.3299

And it is going to contain five lowercase letters that are just strings, a through e.3304

And utilizing a loop counter and the count function, which tells you the number of elements in array,3309

I want you to loop over the lower array, and while you do that, create a new variable called upper.3315

And what that is going to do is: each time you are in the while loop, I want you to change, using the strtoupper function,3323

the value of the current lowercase letter that you are looping over.3330

Change it to uppercase, and set upper equal to that value.3339

So, what you are going to do is: as you go through the while loop, you are going to be building up this upper array3343

that is going to contain all of the letters in lower, but it is going to contain them in uppercase form.3347

And then, go ahead and output upper (using print_r and var_dump) to confirm that upper actually contains the uppercase versions of the lower array.3351

And the reason for doing that is: it is going to give you practice in looping over array using a counter.3361

It is also going to show you that, when you loop over array, you can do different things to it; you can perform different actions.3367

Then, I want you to create an associative array called days that has the keys which are the 3-letter abbreviations3374

for days of the week (so for example, like MON and TUE for Monday and Tuesday).3380

And then, as the values, it is going to have the full name of the days of the week.3386

Then, this time, instead of using an array counter, use the list construct and the each function to loop over this associative array.3390

And create, in the process, a new array called daysReversed, 3400

where the keys of this new array are the values of days, and the values of the array are the keys of days.3405

So, you are basically reversing the key/value pairs.3415

After you exit the loop, go ahead and output daysReversed to confirm that the keys and values were switched.3417

And you can use either the print_r or var_dump function.3424

And that is just to give you practice using this list construct and each construct for looping over associative arrays.3428

That ends today's lesson; thank you for watching Educator.com--I look forward to seeing you next time.3436