PHP if else, if, or if elseif else statements and PHP conditional statements are used to execute different code depending on different conditions. Most of the time, when we write code, we want to execute different codes for different conditions. To do this, we use conditional statements.

  • if : executes the code if the statement/condition is true
  • if ... else: executes some code if the statement/condition is true, another code if it is false
  • if ... elseif ... else: statement/executes different code for more than two conditions

PHP If Syntax

The syntax of the PHP if statement is as in the example below.

if(condition) {
    // koşul true döndürürse çalıştırılacak kodlar
} elseif (condition) {
    // üstteki koşul veya koşullar false döndürürse ve bu koşul true döndürürse çalıştırılacak kodlar
} else {
    // üstteki koşulların hepsi false döndürürse çalıştırılacak kodlar
}

PHP If Example

In the example below, php executes the codes in the if scope if the condition specified in the if statement is true. Otherwise, the elseif condition is queried, and if the elseif condition returns true, the codes in the elseif scope are executed; If it returns false, the next condition is queried. The next condition returns true and the codes in its scope are executed. The reason the last elseif condition returns true is because the expression in the condition is true. The data of variable $number1 (15) is greater than the data of variable $number2.

$number1 = 15;
$number2 = 10;
if($number1 === $number2) {
    echo 'number1 değişkeni verisi ile number2 değişkeni verisi denk';
} elseif ($number1  $number2) {
	echo 'number1 değişkeni verisi, number2 değişkeni verisinden daha büyük'; 
} else {
    echo 'Tespit edilemedi';
}
// Çıktı: number1 değişkeni verisi, number2 değişkeni verisinden daha büyük

PHP ElseIf Syntax

In PHP elseif syntax, elseif can also be written separately instead of combined. However, almost all PHP code standards, especially PSR, recommend writing in unified formats. Additionally, when using the alternative syntax for the PHP if statement, we cannot write the elseif statement separately. If we write else if separately in this way in the PHP if else alternative syntax, it will return an error at the php parse error level.

$time = 18;
if($time === 9) {
    echo 'Saat 9 ve şu an sabah';
} else if ($time === 12) { 
// ayrı yazdık
    echo 'Saat 12 ve şu an öğle';
} elseif ($time === 18) { //birleşik yazdık
    echo 'Saat 18 ve şu an akşam'; 
} else {
    echo 'Saat tespit edilemedi';
}

PHP If Alternative Syntax

PHP offers alternative syntax for control structures, especially the If statement. There are no curly brackets in the PHP if alternative syntax - also known as the colon syntax. Colon and endif; There is. Now let's reinforce this with an example.

$number1 = 10;
$number2 = 10;
if($number1 !== $number2):
    echo 'number1 değişkeni verisi ile number2 değişkeni verisi denk değil';
elseif ($number1  $number2):
	echo 'number1 değişkeni verisi, number2 değişkeni verisinden daha büyük';  
else:
    echo 'Tespit edilemedi';
endif;
// Çıktı: Tespit edilemedi

PHP One-Line If Else Structure

In the PHP if else structure, it is not mandatory to use curly brackets if the codes in the scope are single line. We can see this in the example.

$number1 = 10;
$number2 = 10;
if($number1 !== $number2)
    echo 'number1 değişkeni verisi ile number2 değişkeni verisi denk değil';
elseif ($number1  $number2)
	echo 'number1 değişkeni verisi, number2 değişkeni verisinden daha büyük';  
else
    echo 'Tespit edilemedi';
// Çıktı: Tespit edilemedi

PHP Using Only If

If there is no need for elseif or else conditions in PHP, only if can be used. Let's reinforce this with an example.

$number1 = 10;
$number2 = 10;
if($number1 === $number2)
    echo 'number1 değişkeni verisi ile number2 değişkeni verisi denk';
// Çıktı: number1 değişkeni verisi ile number2 değişkeni verisi denk