PHP-在不运行代码的情况下检查PHP代码的语法错误 [英] PHP - Check PHP Code for Syntax Errors without running the code

查看:274
本文介绍了PHP-在不运行代码的情况下检查PHP代码的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道通过使用eval($code_to_check);我们可以检查此值是否等于FALSE,如果是,我们可以停止脚本的执行.但是,我面临的问题是我无法定义一个函数,因为它被调用了两次...它通过eval()检查进行处理以确保没有语法错误,然后再次处理该函数,但是显示一个错误,指出它无法拒绝该函数,因为它已经在eval'd函数中声明了.我该如何做,以免在EVAL'd函数中声明任何东西,或者也许我们可以在实际调用它之前取消声明eval()函数中声明的所有内容...

I know that by using eval($code_to_check); we can check if this value equals FALSE and if so, we can stop execution of the script. However, the problem I'm facing is that I am unable to define a function, because it gets called twice... it gets processed with the eval() check to be sure there are no syntax errors and than processes the function again, but shows an error that states that it can NOT REDECLARE this function, as it is already being declared in the eval'd function. How can I make it so that we don't declare things in the EVAL'd function, or perhaps, we can undeclare everything that was declared in the eval() function before we actually do call it...

无论如何,到目前为止,这是我目前正在使用的...可以使用一些帮助,因为当$ content(它是php代码)中包含一个函数时,我得到了一个"CAN REDECLARE FUNCTION". /p>

Anyways, here is what I'm working with so far... Could use some help, cause I am getting a "CAN NOT REDECLARE FUNCTION" when $content (which is php code) has a function within it.

// PHP Syntax errors?
if (!@eval('return true;' . $content))
{
    // Error found in PHP somewhere.  Call error function and return out of here!
    call_user_func_array($code_error['function'], $code_error['params']);
    return;
}
else
{
    ob_start();

    eval($content);
    $code = ob_get_contents();
    ob_end_clean();
}

有人可以在这里帮助我吗?谢谢大家,你们在这里非常有帮助!你们都应该获得金牌,但是我相信奥运会已经结束,这还不是一项运动...

Can anyone please help me here? Thanks guys, you are all so very helpful here! You all deserve a GOLD MEDAL, but I believe the Olympics are now over and this isn't quite a sport yet...

好吧,我在这里尝试自己的答案,想知道这是否仍然会捕获错误并允许同时创建函数,而无需两次调用这些函数.这是这样做的正确方法吗?任何人都可以在此代码中看到任何可能的问题吗?如果没有检测到语法错误,我会在$ eval_code中回显...可以这样做吗?

Ok, I am attempting my own answer here, and wondering if this will still catch errors and allow for functions to be created at the same time without calling these functions twice. Is this a proper way of doing this?? Can anyone see any possible problems in this code? I am echoing the $eval_code if no syntax errors detected... is this fine to do?

$eval_code = @eval($content);
// PHP Syntax errors?
if ($eval_code === FALSE)
{
    call_user_func_array($code_error['function'], $code_error['params']);
    return;
}
else
{
    ob_start();
    echo $eval_code;
    $code = ob_get_contents();
    ob_end_clean();
}

推荐答案

$checkResult = exec('echo \'<?php ' . escapeshellarg($code_to_check) . '\' | php -l >/dev/null 2>&1; echo $?');
if ($checkResult != 0) {
    throw new \RuntimeException("Invalid php");
} else {
   $result = eval($code_to_check);
}

这篇关于PHP-在不运行代码的情况下检查PHP代码的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆