Lesson 3
PHP Conditional Statements and Loop Control Basics
Topic Overview

Welcome! In this lesson, we're exploring special instructions in PHP: Conditional Statements, along with the break and continue statements. As we've learned, loops allow us to execute a block of code numerous times. By combining these loops with conditional statements and incorporating the useful break and continue instructions, we achieve more robust and efficient code. Let's dive in!

The 'if' Statement

In PHP, the if statement triggers actions in our code based on a specific condition. Consider this straightforward example, where the if statement determines which message to print based on the value of $temperature:

php
1<?php 2$temperature = 15; 3 4if ($temperature > 20) { 5 echo "Wear light clothes."; // This message will print if the temperature is over 20. 6} else { 7 echo "Bring a jacket."; // This message will print otherwise. 8} 9?>

We can evaluate multiple conditions using elseif. This phrase means, "If the previous condition isn't true, then check this one":

php
1<?php 2$temperature = 15; 3 4if ($temperature > 30) { 5 echo "It's hot outside!"; // This will print if the temperature is over 30. 6} elseif ($temperature > 20) { 7 echo "The weather is nice."; // This will print if the temperature is between 21 and 30. 8} else { 9 echo "It might be cold outside."; // This will print if the temperature is 20 or below. 10} 11?>
The 'break' Statement

We use the break statement whenever we want to exit a loop prematurely once a condition is met:

php
1<?php 2$numbers = [1, 3, 7, 9, 12, 15]; 3 4foreach ($numbers as $number) { 5 if ($number % 2 == 0) { 6 echo "The first even number is: " . $number . "\n"; // This prints the first even number. 7 break; // This stops the loop after printing the first even number. 8 } 9 echo "Number: " . $number . "\n"; 10} 11?>
The 'continue' Statement

The continue statement bypasses the rest of the loop code for the current iteration only:

php
1<?php 2for ($i = 0; $i < 6; $i++) { 3 if ($i == 3) { 4 continue; // This skips the print command for '3'. 5 } 6 echo $i . "\n"; // This prints the numbers from 0 to 5 except 3. 7} 8?>
Use-case with a Foreach Loop

By utilizing the tools we've covered so far, we can craft more flexible loops. Here's a snippet where we terminate the loop once we find "Charlie":

php
1<?php 2$names = ["Alice", "Bob", "Charlie", "David"]; 3 4foreach ($names as $name) { 5 if ($name === "Charlie") { 6 echo "Found Charlie!"; // This prints when 'Charlie' is found. 7 break; // This stops the loop after finding 'Charlie'. 8 } 9} 10?>
Lesson Summary and Practice

Congratulations! You are now familiar with PHP's if statement, as well as the break and continue statements and their applications in loops. We encourage you to reinforce your learning through the practice exercises. Try altering the examples to check different conditions or use continue in different loop scenarios. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.