Where can I read about conditionals done with “?” and “:” (colon)? [duplicate]

It’s the Ternary Operator.

Basic usage is something like

$foo = (if this expressions returns true) ? (assign this value to $foo) : (otherwise, assign this value to $foo)

It can be used for more than assignment though, it looks like other examples are cropping up below.

I think the reason you see this in a lot of modern, OO style PHP is that without static typing you end up needing to be paranoid about the types in any particular variable, and a one line ternary is less cluttered than a 7 line if/else conditional.

Also, in deference to the comments and truth in naming, read all about the ternary operators in computer science.

Leave a Comment