The ternary operator is a version of IF…ELSE
, which consists of grouping, on the same line, the commands of the condition.
traditional condition IF…ELSE
:
$sexo = 'M';
if($sexo == 'M'){
$mensagem = 'Olá senhor';
}else{
$mensagem = 'Olá senhora';
}
condition with ternary operator:
$sexo = 'M';
$mensagem = $sexo == 'M' ? 'Olá senhor' : 'Olá senhora' ;
The first parameter receives an expression, the second is the return if that expression is true, and the latter returns the value if the expression is wrong. Thus the variable $mensagem
gets the value Olá senhor
.
About, notice de undefined
is a simple warning that the variable has not been initialized, ie the variable does not yet have a definition.
Put the code and the error that came up, so it is easier to help you
– Pedro Augusto
Actually I only wanted the explanation of this code: $text = isset($_POST['text']) ? $_POST['text'] : '';
– Guilherme Freitas