Created
November 22, 2013 02:29
-
-
Save wesleysaraujo/7593792 to your computer and use it in GitHub Desktop.
Exemplos de condições ternárias
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Condição tradicional | |
$nota = 4; | |
if ($nota >= 7) { | |
echo "Você passou!"; | |
} else { | |
echo "Você não passou!"; | |
} | |
//Condição Ternária | |
echo ($nota >= 7) ? "Você passou" : "Você não passou"; | |
//Outro exemplo | |
// Atribuição de um valor padrão a uma variável | |
// Versão "padrão" | |
if (!isset($variavel)) { | |
$variavel = 'valor padrão'; | |
} else { | |
$variavel = $variavel; | |
} | |
// Versão usando operador ternário | |
$variavel = (!isset($variavel)) ? 'valor padrão' : $variavel; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment