Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword else

Run the alternative branch only when the associated if or preceding elseif tests fail.

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

Syntax

if ($condition) { /* yes */ } else { /* fallback */ }

Run the alternative branch only when the associated if or preceding elseif tests fail. In nested unbraced statements an else binds to the nearest unmatched if, so braces change which condition owns a fallback. No condition follows else.

function label(bool $open,bool $ready): string { if($open){ if($ready){return "live";} else {return "wait";} } return "closed"; }
echo json_encode([label(true,false),label(false,true)]);
["wait","closed"]

Examples

<?php
function label(bool $open,bool $ready): string { if($open){ if($ready){return "live";} else {return "wait";} } return "closed"; }
echo json_encode([label(true,false),label(false,true)]);

Result

["wait","closed"]

Sources