Advertisement
  1. Code
  2. PHP

El Operador Ternario en PHP

Scroll to top
Read Time: 1 min

() translation by (you can also view the original English article)

What You'll Be Creating

En El Video

El operador ternario nos permite simplificar algunas sentencias condicionales de PHP. Veremos cómo se puede usar, con el desarrollo y refactorización basado en pruebas, para simplificar código como:

1
<?php
2
3
$result = null;
4
5
if (5>3) {
6
    $result = "Bigger";
7
} else {
8
  $result = "Less";
9
}

Usando un operador ternario, podemos escribir la comparación anterior como:

1
<?php
2
$result = 5 > 3 ? "Bigger" : "Less";

Esta es una manera mucho más sencilla para escribir relativamente fácil de entender las sentencias y algo que debemos considerar en el futuro al escribir código,

¡Sé el primero en conocer las nuevas traducciones–sigue @tutsplus_es en Twitter!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.