ternary operation

239 views
Skip to first unread message

Romain Quellec

unread,
Sep 19, 2013, 4:13:03 AM9/19/13
to php-f...@googlegroups.com
Hi.

Simple question : Can we use ternary operation with PSR-2 ? There is no specification about it.

as a reminder, ternary expression : (expr1) ? expr2 : expr3

guilher...@gmail.com

unread,
Sep 19, 2013, 11:51:44 AM9/19/13
to Romain Quellec, php-f...@googlegroups.com
I normally tend to use like:

(expr)
    ? expr2
    : expr3;


--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group - Coding Style" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig-cs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Guilherme Blanco
MSN: guilher...@hotmail.com
GTalk: guilhermeblanco
Toronto - ON/Canada

Charlie Schliesser

unread,
Jan 22, 2014, 12:01:14 PM1/22/14
to php-f...@googlegroups.com, Romain Quellec

I think since there's no spec that means it's to be used at your discretion.


If breaking to multiple lines, I would use a control structure.

Benjamin Delespierre

unread,
Mar 6, 2014, 10:39:04 AM3/6/14
to php-f...@googlegroups.com, Romain Quellec
IMHO a control structure add more lines, deteriorate lisibility, and doesn't improve performances.

$a = (expr)
  ? $b
  : $c;

vs.

if (expr)
  $a = $b;
else
  $a = $c;

Note you can also use the contracted form of the ternary operator:

$a = $b ?: $c;

which is very practical to pick a default value.
Reply all
Reply to author
Forward
0 new messages