for, foreach, switches Flashcards

1
Q

<?php foreach ($array as $item) : ?>
<!— Echo item —!>
<? endforeach; ?>

A

Iterates over each element in $array, assigning the current elements value to $item for each loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of a switch statement in PHP

A

A switch statement is used to perform different actions based on different conditions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a PHP switch statement that echoes “You did okay” if the variable $grade is equal to “C”

A

switch($grade) {
case “C”:
echo “You did okay”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Implement a PHP switch statement that assigns a message based on a range of numerical grades.

grades 90 and above, echo “Excellent”;
grades 80-89, echo “Very Good”;
grades 70-79, echo “Good”;
grades 60-69, echo “Average”;
grades below 60, echo “Below Average”;

A

switch($grade) {
case $grade >= 90:
echo “Excellent”;
break;
case $grade >= 80 && $grade < 90:
echo “Very Good”;
break;
case $grade >= 70 && $grade < 80:
echo “Good”;
break;
case $grade >= 60 && $grade < 70:
echo “Average”;
break;
default:
echo “Below Average”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly