Ternary operator returns error 500

Asked

Viewed 39 times

0

I wanted it on condition whenever the input text is equal to an off value input radio, but always returns error 500 when running on the page:

$y = 0;
while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {   
    $property = ($rows_cursos["Descricao"] === "1 Desinfetante de Surpeficies" || $rows_cursos["Descricao"] === "Saco/Caixa c/ Cotonetes") : "disabled" ? "required";
    $tabela1 .= '<tr>';
    $tabela1 .= '<td> <input type="text" readonly="true" size="20" name= "Produto['.$y.']" class= "Produto" value="'.$rows_cursos['Descricao'].'"></td>';
    $tabela1 .= '<td> <input type="radio" name= "DataP['.$y.']" value="Ok" '.$property.'></td>';
    $tabela1 .= '<td> <input type="radio" name= "DataP['.$y.']" value="Não Ok" '.$property.'></td>';
    $tabela1 .= '</tr>'; 
    $y++;
}
  • This code works without the ternary operator?

  • @Bruno Romualdo yes it works, the problem is when I add :"disabled"?"required"

  • do not forget to mark an answer as correct, and I advise that while learning something new always research and write the code yourself so that you record better and can help others when they need it.

2 answers

3


Error 500 is an internal server error. To know where the error is you need to enable the error log on the server or enable the display errors in the script by putting the following code at the beginning:

error_reporting(E_ALL);
ini_set('display_errors', 1);

In your case the error is in the ternary operator. It is a very common mistake to change the position of the : with the ?. Of documentation:

The Expression (Expr1) ? (expr2) : (expr3) evaluates to expr2 if Expr1 evaluates to TRUE, and expr3 if Expr1 evaluates to FALSE.

Changing line 5 of your code should solve error 500.

$property = ($rows_cursos["Descricao"]==="1 Desinfetante de Surpeficies" || $resultado_cursos["Descricao"]==="Saco/Caixa c/ Cotonetes") ? "disabled" : "required";

A tip is to think about expr1 as a question. This reminds you that at the end has the question mark.

2

Ta wrong order buddy, operator is like this:

condição ? resultado_verdadeiro : resultado_falso

you put the ? in place of :.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.