PHP

PHP Menu

PHP

Conditionals - PHP Basics

When writing code, you frequently want to do different actions for different conditions. To do this, you can utilize conditional statements in your code.

PHP have the following conditional statements:

  • if statement - executes some code if one condition is true
  • if...else statement - executes some code if a condition is true and another code if that condition is false
  • if...elseif...else statement - executes different codes for more than two conditions
  • switch statement - selects one of many blocks of code to be executed

The if Statement

The if statement is used to execute a block of code only if the specified condition evaluates to true.

The example below outputs "Happy Friday!" if $today variable is equal to Friday.

<?php
$today = "Friday"; if ($today == "Friday") { echo "Happy Friday!"; }

The if...else Statement

The if...else statement executes one block of code if the specified condition is evaluated as to true and another block of code if it is evaluated to be false.

The example below outputs "Good day!" if $mytime is less than 20, otherwise it outputs "Good night!". Try changing the value of $mytime.

<?php
$mytime = 12;
if ($mytime < 20) { echo "Good day!"; } else { echo "Good night!"; }

The if...elseif...else Statement

The if...elseif...else statement executes different codes for more than two conditions.

The example below outputs "Good morning!" if $mytime is less than 10, "Good day!" if less than 20 or "Good night!" if greater than or equal 20. Try changing the value of $mytime.

<?php
$mytime = 8;
if ($mytime < 10) { echo "Good morning!"; } elseif ($mytime < 20) { echo "Good day!"; } else { echo "Good night!"; }

The Ternary Operator

The ternary operator provides a shorthand way of writing the if...else statements. The ternary operator, question mark (?) symbol, takes three operands: a condition to check, a result for true, and a result for false.

The example below shows an if...else statement and its equivalent using the ternary operator. Try to change the value of $age.

<?php
$age = 19; if ($age < 18) { echo 'Minor'; // Display Minor if age is less than 18 } else { echo 'Adult'; // Display Adult if age is greater than or equal to 18 } echo "<br>"; echo ($age < 18) ? 'Minor' : 'Adult'; // same statement as above but using the ternary operator

Exercise

Create a variable $grade. Write the code that displays Passed when $grade is greater than 60. To test your code, assign a value greater than 60 to the variable $grade.

<?php
$var = 123;
<?php
$grade = 70; if ($grade > 60) { echo "Passed"; }
{ "test_output_contains":{ "expected":"Passed", "error_message":"Did you assign the correct value for <code>$grade<\/code>?" }, "test_variable_exists":{ "object":"$grade", "error_message":"Have you declared <code>$grade<\/code>?" }, "success_message":"Good job!", "error_message":"Please read the instructions again." }

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods