DONATE Hyvor Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Conditionals

Conditionals are the most important features of a programming language.

Conditionals are used to perform different actions on different conditions.

PHP Conditional Statements

In PHP, we have the following conditional statements:

Conditional statements are case-insensitive

PHP If Statement

if Syntax

if (condition) { code group }

if statement executes the code group inside {}, if the condition inside () returns true.

  • Code group is a group of statements which needed to be executed if the condition is true.
  • Condition is an expression which returns a Boolean value. Here you can use the operators we learned in the last chapter.

PHP If Statement Example


<?php
$day = date('j'); // day of the month
if ($day < 15) {
	echo 'You are spending the first half of the month'; 
}

Run Example ››

If you do not see any output in the above example, you are spending the last half of the month. Now we need to say that too. The else statement can be used for that.

PHP If-Else Statement

if (condition) { code to be executed if the condition is true } else { code to be executed if the condition is false }

PHP If-Else Statement Example


<?php
$day = date('j'); // day of the month
if ($day < 15) {
	echo 'You are spending the first half of the month'; 
} else {
	echo 'You are spending the last half of the month';
}

Run Example ››

PHP If-Elseif-Else Statement

if-elseif-else statement allows you to execute different code groups based on different conditions.

if (condition 1) { code to be executed if the condition 1 is true } elseif (condition 2) { code to be executed if the condition 1 is false, but condition 2 is true } else { code to be executed if both condition 1 and 2 are false }
In PHP, both elseif and else if are valid.

The following code returns a string describing which quarter of the month you are spending.

PHP If-Elseif-Else Statement Example


<?php
$day = date('j'); // day of the month

if ($day >= 21) {
	$quarter = 'last';
} else if ($day >= 14) {
	$quarter = 'third';
} else if ($day >= 7) {
	$quarter = 'second';
} else {
	$quarter = 'first';
}

echo 'You are spending the ' . $quarter . ' quarter of the month';

Run Example ››

You can use any number of elseif statements.

In the following example, we use a number of elseif statements to display different messages in different conditions. Also, you will notice that the else statement at the end is not necessary for this. You can omit it when not needed.

PHP Using Multiple Elseif


<?php
$randomScore = rand(0,4); // random score between 0-4
if ($randomScore === 0) {
	echo '0 Points, please try again';
} elseif ($randomScore === 1) {
	echo '1 Point, Try more';
} elseif ($randomScore === 2) {
	echo '2 Points, Nice!';
} elseif ($randomScore === 3) {
	echo '3 Points, One more to reach the best';
} elseif ($randomScore === 4) {
	echo '4 Points, You won!';
}

Run Example ››

Tip: The rand() function generates a random number. Arguments are min and max respectively.

PHP Switch Statement

In many places, you may need to compare the same expression (or variable) with different values. In this case, The switch statement is seriously essential.

switch(expression) { case value1: code to execute if expression = value1 break; case value2: code to execute if expression = value2 break; ... default: code to execute if expression is not equal to any value above }

Let's change the last example (Random Scoring) to have switch statements instead of if-elseif.

PHP Switch Example


<?php
$randomScore = rand(0,4); // random score between 0-4
switch ($randomScore) {
	case 0:
		echo '0 Points, please try again';
		break;
	case 1:
		echo '1 Point, Try more';
		break;
	case 2:
		echo '2 Points, Nice!';
		break;
	case 3:
		echo '3 Points, One more to reach the best';
		break;
	case 4:
		echo '4 Points, You won!';
		break;
}

Run Example ››

Note: The break statement is used to jump out from the switch statement. See the loop control structure chapter to learn more.

Using Logical Operators in If Statements

Logical operators are commonly used with If statements.

PHP AND Example


<?php
$a = true;
$b = true;
if ($a and $b) {
	echo 'Both $a and $b are true';
}

$a = false;
$b = true; 
if ($a and $b) {
	echo 'This is not echoed because $a is false';
}

Run Example ››

PHP && Example


<?php
$a = true;
$b = true;
if ($a && $b) {
	echo 'Both $a and $b are true';
}

$a = false;
$b = true; 
if ($a && $b) {
	echo 'This is not echoed because $a is false';
}

Run Example ››
Both AND and && are identical

PHP OR Example


<?php
$a = true;
$b = false;

if ($a or $b) {
	echo 'Either $a or $b (or both) is true';
}

Run Example ››

PHP || Example


<?php
$a = true;
$b = false;

if ($a || $b) {
	echo 'Either $a or $b (or both) is true';
}

Run Example ››
Both OR and || are identical

PHP XOR Example


<?php
$a = true;
$b = false;

if ($a xor $b) {
	echo 'Either $a or $b is true. But not both';
}

$a = true;
$b = true;

if ($a xor $b) {
	echo 'This is not echoed because both $a and $b are true';
}

Run Example ››

PHP ! (NOT) Example


<?php
$a = false;
if (!$a) {
	echo '$a is false. So, !$a is returns true';
}

$a = true;
if (!$a) {
	echo 'This is not echoed because $a is true';
}

Run Example ››

Nested If Statements

We can use if statements inside if statements. These statements are called Nested If Statements.

PHP Nested If Example


<?php
$a = 10;
if ($a >= 0) {
	echo 'Positive Number <br>';
	if ($a % 10 === 0) {
		echo 'The number is a multiple of 10';
	}
} else {
	echo 'Negative Number. Please enter a positive one';
}

Run Example ››

Tip: a % b returns the remainder of a divided by b while a and b are Numbers.

Any number of if statements can be nested.
0
192