Jump to content

[CodeIgniter] Ternary Operator as array element causing white screen


sKunKbad

Recommended Posts

I've posted here because the set_value() function in the code below is exclusive to CodeIgniter.

 

I've used ternary operators as array elements before, by simply wrapping them in parenthesis. What's confusing to me is that on my development environment, which is Ubuntu w/ PHP 5.5.X, everything works fine, but the production environment is some Linux hosting PHP 5.4.41, and it just gives a white screen. This happens without any error displayed, and happens even when the code is not executed, just simply loading the code (syntax error?).

 

The code causing white screen:
 

<?php
$arr = [
    'a' => ( ! empty( set_value('a') )
        ? date('Y-m-d', strtotime( set_value('a') ) )
        : NULL ),
    'b' => ( ! empty( set_value('b') )
        ? date('Y-m-d', strtotime( set_value('b') ) )
        : NULL ),
    'c' => ( ! empty( set_value('c') )
        ? date('Y-m-d', strtotime( set_value('c') ) )
        : NULL ),
    'd' => ( ! empty( set_value('d') )
        ? date('Y-m-d', strtotime( set_value('d') ) )
        : NULL )                            
];

So, why would my development environment be OK with this code but production have a white screen of death?

 

Link to post
Share on other sites

Error logs don't show anything. I can replicate this by running the following script:

<?php

function set_value($x)
{
    return FALSE;
}

$arr = [
    'a' => ( ! empty( set_value('a') )
        ? date('Y-m-d', strtotime( set_value('a') ) )
        : NULL ),
    'b' => ( ! empty( set_value('b') )
        ? date('Y-m-d', strtotime( set_value('b') ) )
        : NULL ),
    'c' => ( ! empty( set_value('c') )
        ? date('Y-m-d', strtotime( set_value('c') ) )
        : NULL ),
    'd' => ( ! empty( set_value('d') )
        ? date('Y-m-d', strtotime( set_value('d') ) )
        : NULL )                            
];

var_dump( $arr );

On production I get the white screen of death.

 

On development I get:

 

array(4) { ["a"]=> NULL ["b"]=> NULL ["c"]=> NULL ["d"]=> NULL }

 

Link to post
Share on other sites

After putting your code in my IDE, it alerted me to the following: "Arbitrary expressions in empty are allowed in PHP 5.5 only". Until PHP 5.5, you cannot evaluate functions inside of empty(). You can only use a variable.

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.