-
Notifications
You must be signed in to change notification settings - Fork 0
/
aula11.php
39 lines (23 loc) · 742 Bytes
/
aula11.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
// Aula 11 - Operador ternário
// Funciona da seguinte forma:
// $variavel = (expressão) ? faça x : faça y;
// Explicação do código acima: Temos uma variável, essa variável irá receber uma expressão, se essa expressão for verdadeira, faça x, se não for verdadeira, faça y.
// Como ficaria um código sem o operador ternário:
// Descobrindo se um número é par ou ímpar
/*
$n = 10;
$tipo = '';
if ($n % 2 == 0) {
$tipo = 'par';
}
else{
$tipo = 'ímpar';
}
echo $tipo;
*/
// Agora, o código abaixo será o mesmo escrito acima com o operador ternário, ou seja, de foma mais simplificada.
$n = 11;
//$tipo = $n % 2 == 0 ? 'par' : 'ímpar';
$tipo = !($n > 0) ? 'Negativo' : 'Positivo';
echo $tipo;