Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP VariablesPHP ConstantsPHP OutputPHP Data TypesPHP 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.

PHP Conditional Statements

In PHP, we have 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 }

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

PHP If-Elseif-Else Statement Example


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

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

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

Run Example ››

You can use any number of elseif statements.

In PHP, both elseif and else if are valid.

PHP Switch Statement

In many occasions, you may need to compare the same expression (or variable) with different values. In this case, PHP switch statements are 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 }

The logic of switch statements:

  • First, we define an expression (most often a variable) for the statement.
  • Then, we have some case blocks inside the switch statement.
  • Those case blocks have a value to check with it. (value1, value2...)
  • If the value of expression matches the value of a case block, that case block will be executed.
  • If no match found, default statement will be executed. (This is optional)
  • We use break statement to specify the end of a case block. (You should always use this to prevent the automatic execution of the next case block.)
Advertisement
0
149