Skip to content
Advertisement

Tag: conditional-operator

Is there a fancy and short solution for using the null coalescing operator with constants in PHP?

Since PHP 8 the constant() function throws an error instead of returning null and generating an E_WARNING when the given constant is not defined, and therefore I can’t use the following code to set a variable anymore: My obvious quick solution for this is changing that line to: It honestly bothers me a little because I like how the first

How to display a column of data when the parent variable might not be declared?

I am generating some dynamic content in the view layer of my codeigniter project. The trouble is, I am getting the following error in my ternary expression: Parse error: syntax error, unexpected token “foreach” How can I display the looped category_name data separated by <br/> tags without generating this error when $log->category is not declared? Answer The code you have

Nested PHP ternary operator precedence

Yes, I know this is very bad code, but I’d still like to understand it: If $a is 11, then the result of the 1st line is “option 2”, but in the 2nd line, the result is “option 1” – what effect is the pair of brackets having? Answer The first line is parsed like so: Which is equivalent to

Advertisement