Strange php behaviour in codeigniter 2 when running on php 7 - implode function

Viewed 81

We are in process of upgrading from php 5.6 to php 7.

We've been facing some very strange issues one of which is :

in the CI active record class - system\database\DB_active_rec.php - function _compile_select()

Context: User submits a login form with username and password, ajax request sent to the server.

This line of code behaves unexpectedly:

$sql .= implode(', ', $this->ar_select); // value of $this->ar_select is ['Type', 'Value']

If I change this to :

$str = implode(', ', $this->ar_select);
$sql .= $str;

it works fine.

When I say it doesn't work I mean there is no response from the server.Chrome network tab shows: (failed)net::ERR_EMPTY_RESPONSE

Tried placing the code in a try - catch but no exception were thrown. No errors in apache error_log.

In addition, why trying to debug, if I add an if condition without an else block, it doesn't work (i.e. not executing the block after if) even when the condition is evaluated to true.

Adding a non-empty else block gets it working where the code in the if condition runs.

1 Answers

You are concatenating a string with var that doesn't exist yet:

$sql .= implode(', ', $this->ar_select); 

In second example you initialized a $sql var and then concatenate it which is ok.

You should see an error in PHP error log like:

Notice: Undefined variable:

if you enable error logging in PHP.

PHP 7 treats more strictly undefined vars then PHP 5.

Related