⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

Conditional Statements

Conditional statements are used to perform different actions based on different conditions. Very often when writing code, you want to perform different actions for different conditions, which is made possible by statements such as if, else, and switch. They give your program the ability to "make decisions" on which path to take.

EXAMPLE
RESULT
The result will be displayed here...

Overview of Conditional Statements in PHP

In PHP, we have three main ways to check conditions and direct the flow of the program:

  1. if: The most basic statement. It executes a block of code inside curly braces only if the specified condition evaluates to true (true).
  2. if...elseif...else: Allows checking multiple consecutive conditions. The code block that corresponds to the first true condition will execute. If none of the conditions are met, the default code inside the else block will run.
  3. switch: An elegant and cleaner alternative to the if-elseif structure when you want to check a single variable against multiple distinct fixed values (known as cases or case statements).

🛠️ Essential Tools for Conditions: Comparison and Logical Operators

To write a condition inside the parentheses of an if statement, we use operators. Here are the ones you need to know: