This is tested against PHP 7.4 I’m trying to understand why the first example does not return ‘hello’ like the other two. Example 3 is the same thing as example 1 except the ternary is encapsulated in parentheses but for some reason the presence of the parentheses on example 3 give the expected result. Example 2 is the same as
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