for, foreach, switches Flashcards
<?php foreach ($array as $item) : ?>
<!— Echo item —!>
<? endforeach; ?>
Iterates over each element in $array, assigning the current elements value to $item for each loop.
What is the purpose of a switch statement in PHP
A switch statement is used to perform different actions based on different conditions
Write a PHP switch statement that echoes “You did okay” if the variable $grade is equal to “C”
switch($grade) {
case “C”:
echo “You did okay”;
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”;
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”;
}