Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword elseif

Test another condition only after earlier branches failed.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

if ($a): /* first */ elseif ($b): /* second */ endif;

Test another condition only after earlier branches failed. The first truthy condition wins and later tests are skipped. With the colon syntax, use the single keyword elseif; the two-word else if form used with braces does not fit this syntax.

function band(int $n): string { if($n>=10): return "high"; elseif($n>=5): return "mid"; else: return "low"; endif; }
echo json_encode([band(12),band(7),band(2)]);
["high","mid","low"]

Examples

<?php
function band(int $n): string { if($n>=10): return "high"; elseif($n>=5): return "mid"; else: return "low"; endif; }
echo json_encode([band(12),band(7),band(2)]);

Result

["high","mid","low"]

Sources