PHP Ternary conditions

1

I was wondering if there was any way to perform a ternary codicional that only evaluates a true condition and not a false one.

For example, the ternary conditional in php would be:

$n = (2) ? true : false;

My idea would be to make a ternary conditional such that:

$n = (2) ? true;

I have tried different ways but they all give me an error, so I guess there is no way, but just in case I ask around here.

Thanks! :)

I put the entire code without the previous example:

function encrypt($chr) {

  if($chr === 'Ñ') { return 0; }

  $abc = array(    'A' => 2,    'B' => 22,
                   'C' => 222,  'D' => 3,
                   'E' => 33,   'F' => 333,
                   'G' => 4,    'H' => 44,
                   'I' => 444,  'J' => 5,
                   'K' => 55,   'L' => 555,
                   'M' => 6,    'N' => 66,
                   'O' => 666,  'P' => 7,
                   'Q' => 77,   'R' => 777,
                   'S' => 7777, 'T' => 8,
                   'U' => 88,   'V' => 888,
                   'W' => 9,    'X' => 99,
                   'Y' => 999,  'Z' => 9999 );

      return $abc[$chr];
}

My idea was simply to use a ternary instead of an if, (for aesthetics) I know it's silly. That's why I was wondering if you could do something like:

return $chr == 'Ñ' ? true : false;

The thing is that I only want to return the true, not the false, if it's false, my idea is to continue with the code.

    
asked by Omar 09.07.2018 в 15:15
source

2 answers

1

By the very nature of the ternary operator, you can not make a ternary operator that only returns true in case the expression to be evaluated is true and nothing in case it is false.

  

As of PHP 5.3, it is possible to set aside the middle part of the   ternary operator. The expression expr1 ?: expr3 returns expr1 if expr1   it is evaluated as TRUE and expr3 if it is otherwise.

Therefore the only thing that could be done:

$n = 2;
/* Si la expresion a evaluar es verdadera devuelve true 
pero si no , devolverá false (o lo que le indiques)*/
$resultado = ($n === 2) ?: false;  
$resultado; // true

$resultado = ($n === 5) ?: false;  
$resultado; // false

Which is the same as:

$n = 2;
$resultado = ($n === 2) ? true : false;
$resultado; // true

$resultado = ($n === 5) ? true : false;
$resultado; // false

Here in the official documentation of PHP there is a section that talks about the ternary operator: Comparison Operators PHP

    
answered by 09.07.2018 в 16:01
0

The same word says it, ternary. The operation must be composed of 3 parts, the evaluation, and the two types of return. For syntax $n = 2 ? true; is wrong and will return errors, that's why its structure should not be violated following the pattern:

  

Evaluation? If evaluation is true: If evaluation is false;

//Comparación booleana
$check = false;
$isChecked = $check ? true : false;

//Comparación numérica
$num = 5;
$isGrater = $num > 5 : true : false;

//Dos o más validaciones
$num = 5; 
$isChecked = true;
$checkedAndGreater = $num > 5 && $isChecked ? true : false;

//Anidación ternaria
$getId = false;
$age = 18;    
$barAccess = $getId ? 'Bienvenido' : $age === 18 ? 'Bienvenido, cerveza?' : 'No puedes pasar';

//Recuerda que en boolean cualquier número diferente de 0 es TRUE, por lo que si evalúas el 2, será siempre TRUE, la forma correcta sería
$n = 2;
$n = $n === 2 ? 'Es dos' : 'No es dos;

All cases must have 3 parts, if it would not stop being a ternary evaluation, you can not break that syntax.

Greetings

    
answered by 09.07.2018 в 15:49