Tutorial :Need a php script diagnosis for a small snippet of code



Question:

Can somebody tell me what I am doing wrong really? I am going nuts, the following code works perfect on localhost/WIN and when I try it on the webhost/linux it gives warnings:

$lines = file('english.php');  foreach($lines as $line) {      $matches=array();      if (preg_match('/DEFINE\(\'(.*?)\',\s*\'(.*)\'\);/i', $line, $matches)) {          $keys[] = $matches[1];          $values[] = $matches[2];      }  }  $lang = array_combine($keys, $values);  

When I test on webhost:

Warning: array_combine() expects parameter 1 to be array, null given in /home/xx/public_html/xx on line 616  

But on local server (windows xp) it works perfect. I do not know what I am doing wrong, please help me resolve this nightmare :(

Thanks.


Solution:1

I don't see anything obviously wrong with your code, but I'm curious why you're building separate arrays and then combining them rather than just building a combined array:

// Make sure this file is local to the system the script is running on.  // If it's a "url://" path, you can run into url_fopen problems.  $lines = file('english.php');    // No need to reinitialize each time.  $matches = array();    $lang = array();  foreach($lines as $line) {      if (preg_match('/DEFINE\(\'([^\']*)\',\s*\'([^\\\\\']*(?:\\.[^\\\\\']*)*)\'\);/i', $line, $matches)) {          $lang[$matches[1]] = $matches[2];      }  }  

(I've also changed your regex to handle single quotes.)


Solution:2

Are the php versions the same?

And are you sure you have transfered all your files to the webhost?


Solution:3

It seems your $keys variable is null, because you're not initializing it anywhere.

My best guess is that the english.php file on your server is empty (or does not exists), so when you try to read it nothing is saved in $keys variable;

Try adding an initial value for that variable before the foreach statement:

$lines = file('english.php');  $keys = array();  foreach($lines as $line) {  $matches=array();      if (preg_match('/DEFINE\(\'(.*?)\',\s*\'(.*)\'\);/i', $line, $matches)) {          $keys[] = $matches[1];          $values[] = $matches[2];      }  }  $lang = array_combine($keys, $values);  

That way, even if the file doesn't exist or is empty you're covering all possible paths.

You should always code as if everything could go wrong, not the other way around :)


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »