Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / PHP

Palavra-chave elseif

Testa outra condição após falharem as anteriores.

Versão: PHP 8.4; repository runtime, production runtime not attested

Sintaxe

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

Testa outra condição após falharem as anteriores. A primeira verdadeira vence e as seguintes são ignoradas. A sintaxe com dois pontos exige a palavra elseif, a forma else if permitida com chavetas não serve aqui.

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"]

Exemplos

<?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)]);

Resultado

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

Fontes