w1coding.comAll tutorials

PHP Control

PHP lessons
  1. Environment Setup
  2. Introduction
  3. Variables
  4. Basics
  5. Operators
  6. Control
  7. Loops
  8. Variables
  9. Functions
  10. Arrays
  11. Superglobal
  12. File
  13. Loops
  14. Date
  15. Loops
  16. Strings
  17. Error
  18. Class
  19. File
  20. Include
  21. Sessions
  22. Mysql
  23. Cookies
  24. Forms
  25. Cookies
  26. Regular
  27. Authenthication
  28. Fileupload
  29. Mvc
  30. Responsive

Control Structures in PHP

Control structures allow you to control the flow of execution of your PHP code. They are used to make decisions, repeat actions, and more.

If Statement

The if statement executes code if the condition is true:

      
        <?php
          $age = 18;
          if ($age >= 18) {
            echo "You are an adult.";
          }
        ?>
      
    

Else If and Else

You can also use else if and else to create more complex conditions:

      
        <?php
          $age = 16;
          if ($age >= 18) {
            echo "You are an adult.";
          } else if ($age >= 13) {
            echo "You are a teenager.";
          } else {
            echo "You are a child.";
          }
        ?>
      
    

Activity

Try It Yourself!

Write an if statement that checks if a number is even or odd.

Quiz

Quick Quiz

  1. What is the purpose of the else if statement?
  2. What happens if none of the conditions in an if block are true?

Answers: The else if is used to test another condition; the else block will execute if none of the previous conditions are true.