Check for PHP Syntax errors?

馋奶兔 提交于 2020-11-27 05:08:01

问题


Well, I have run into a bit of a pickle here. I am needing to check some PHP for syntax errors. I noticed this bit that needs to run from the commandline:

php -l somefile.php

However, is there a way to run this from within a PHP file itself? I've been looking and have think that I can use parse_str function somehow to accomplish this by entering it into a $_GET, but can't quite understand how this works.

Someone else told me to use token_get_all() php function to determine this.

But I can't figure out how to do this with any approach? Can anyone here give me some sample code to get started perhaps?? I don't think using eval() is the way to go, although I had an eval($code) working, but don't think I should run the script if there are PHP syntax errors.

Any help on this is greatly appreciated, as always!


回答1:


You could simply do shell_exec() like this:

$output = shell_exec('php -l /path/to/filename.php');

This gives you the output of the command line operation in the string $output.




回答2:


It is safer to check the return status of php -l

$fileName = '/path/to/file.php';
exec("php -l {$fileName}", $output, $return);

if ($return === 0) {
    // Correct syntax
} else {
    // Syntax errors
}

See this fiddle to see it in action




回答3:


I use token_get_all for this. I have some PHP code in the db. Before saving, I do

function is_valid_php_code_or_throw( $code ) {
        $old = ini_set('display_errors', 1);
        try {
                token_get_all("<?php\n$code", TOKEN_PARSE);
        }
        catch ( Throwable $ex ) {
                $error = $ex->getMessage();
                $line = $ex->getLine() - 1;
                throw new InvalidInputException("PARSE ERROR on line $line:\n\n$error");
        }
        finally {
                ini_set('display_errors', $old);
        }
}

Works like a charm. Syntax only. No missing variables, type incompayibility etc.

InvalidInputException is my own. You can make it anything, or return a bool, or handle the exception yourself.

I'm not sure if display_errors is necessary. It was at some point.




回答4:


php_check_syntax should do the trick. If you're running PHP >= 5.05, see the first comment in the comments section for the implementation.




回答5:


I would do it like this:

$php_file = 'The path to your file';
if(substr(`php -l $php_file`, 0, 16) == 'No syntax errors') {
    // Correct syntax
} else {
    // Error
}



回答6:


Why use the shell at all?

function syntax_is_valid($code)
{
    try
    {
        @eval($code);
    }
    catch (ParseError $e)
    {
        return false;
    }

    return true;    
}

Alternatively use $e->getMessage() for more info.




回答7:


You can use exec to check for syntax errors.

$tempFile = path/of/file
$syntaxParseError = strpos(exec('php -l '.$tempFile), 'No syntax errors detected') === false;`

Unfortunately, this will not give you the line number or tell you anything about the error. For that you will either need to install static analyzer on your server Is there a static code analyzer [like Lint] for PHP files? or write your own parser.

NB. token_get_all() will not determine anything on its own, but it useful function for making a parser.



来源:https://stackoverflow.com/questions/11960666/check-for-php-syntax-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!