Including files with possible syntax errors...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alexmila
Forum Newbie
Posts: 4
Joined: Sun Feb 01, 2009 12:37 pm

Including files with possible syntax errors...

Post by alexmila »

Hi Guys,

I'm working on script that includes others scripts in another folder which they could have a syntax error. I'm reading php arrays in those scripts. What I want to do it's to catch that error when before opening or while opening the script so I can prevent the script from stopping.

For example:

Code: Select all

 
<?php
$arr = array("one")=>1, "two"=> 2 "three"=>3);
?>
 
The previous array is missing a comma after the number two. I tried to catch that error with an error handler but it still stops the script and It doesn't catch the error.

A handler to catch errors would look something like this:

Code: Select all

 
<?php
set_error_handler('oops');
 
 
function oops($type, $msg, $file, $line, $context) {
 
echo $type.$msg.$file, $line.$context;
} 
?>
 
So if I do:

Code: Select all

 
<?php
set_error_handler('oops');
 
$arr = array("one")=>1, "two"=> 2 "three"=>3);
 
 
 
function oops($type, $msg, $file, $line, $context) {
 
echo $type.$msg.$file, $line.$context;
} 
?>
 
I get a warning and the script stops.

Any ideas? All possible help will be appreciated, Thanks
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: Including files with possible syntax errors...

Post by Randwulf »

You could test the script before including it. There's probably a better way of doing what you need done, but I believe this would work.

you could do a file_get_contents("http://www.url.com/index.php?arg1=efe&arg2=egwrg") and if it contains "PHP Error" (or whatever unique phrase you get from erroneous scripts, I forget what it is exactly) then you don't include it.
alexmila
Forum Newbie
Posts: 4
Joined: Sun Feb 01, 2009 12:37 pm

Re: Including files with possible syntax errors...

Post by alexmila »

I tried that already and didn't work, I keep getting the same warning.

Code: Select all

 
<?php
 
if(file_get_contents($file))
    {
        include_once $file; 
        }else{
               echo "failure";
        }
 
?>
 

Error:

Code: Select all

 
Parse error: parse error, expecting `')'' in....line 8
 
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: Including files with possible syntax errors...

Post by Randwulf »

I may be wrong, but I don't think file_get_contents fails if the PHP script it's 'getting' from fails. I think it will return the error the script crashed with as the string.

So you could do something like:

Code: Select all

 
$str = file_get_contents("index.php");
 
if (strpos($str, "error:") != 0)
include("index.php");
else
die ("Error");
alexmila
Forum Newbie
Posts: 4
Joined: Sun Feb 01, 2009 12:37 pm

Re: Including files with possible syntax errors...

Post by alexmila »

Randwulf! great idea now it works!

Code: Select all

 
 
 $str = file_get_contents($file);
          
         
         if (eval($str)){
             $arr=eval($str);   
             print_r($arr);
        }
         
 
 
I get the string from file_get_contents then I evaluate it as php code, if it doesn't return false I associate it to a variable and use it. I still get a parse error on the screen but the script doesn't stop now!

Bad thing that set_erro_handler cant catch syntax errors using eval. Working on this now. Thanks..
Post Reply