Jump to content

Archived

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

sKunKbad

[CodeIgniter] Ternary Operator as array element causing white screen

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?

 

Share this post


Link to post
Share on other sites

White screen is generally a fatal error. Check the Apache error log.

Share this post


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 }

 

Share this post


Link to post
Share on other sites

Ah, I see you are doing $arr = [];. That syntax is new to PHP 5.4. What version are you running on your production?

Share this post


Link to post
Share on other sites

Ah, I see you are doing $arr = [];. That syntax is new to PHP 5.4. What version are you running on your production?

 

5.4.41

Share this post


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.

Share this post


Link to post
Share on other sites

Cool, thanks for the answer. It would be nice if PHP would have at least spit out some kind of an error.

Share this post


Link to post
Share on other sites

It should have. Your error reporting settings are probably not set up properly on your production config.

Share this post


Link to post
Share on other sites

I tried:

error_reporting(E_ALL);
ini_set("display_errors", 1);

Still nothing, just white screen. Nothing logged.

Share this post


Link to post
Share on other sites

It's because the error is occurring before that code is executed. I'm betting that errors are turned off in your php.ini.

Share this post


Link to post
Share on other sites

×
×
  • 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.