In this lesson our instructor gives an introduction to conditional loops. First, he discusses while loop, looping over arrays, and array traversal functions. Then he talks bout looping over indexed and associative arrays. He also lectures on looping over arrays using list() and each(), control structure scope and coding conventions. He ends the lesson with a helpful homework challenge.
A while loop is a conditional control structure that executes a
statement group repeatedly as long as its specified test condition remains
A while loop’s test condition’s value is compared to TRUEbefore 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
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.
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.