Convert "if (isset ())" to the conditional "? : "(Ternary operator)


I have this code:

if (isset($array[$a]['estado']))
    echo "True";
    echo "False";

I want to know if I can pass it to the conditional format ? :

asked by Mariano 16.11.2016 в 10:50

2 answers


To give you a little more information, what you're looking for is called ternary operators and yes, they can be done in PHP.

These ternary operators work in the following way:

condicion ? resultadoTrue : resultadoFalse

Therefore, translating your if , as commented @Muriano , would be:

echo isset($array[$a]['estado']) ? "True" : "False"

If you have a version of PHP 5.3 or higher, you could delete the intermediate part of the ternary operator, leaving:

condicion ?: resultadoFalse

And it would return the value of condicion if true or resultadoFalse in case the condition was false.

answered by 16.11.2016 / 12:31
echo isset($array[$a]['estado']) ? "True" : "False";
answered by 16.11.2016 в 10:52