Php if($ var)曾经工作过 [英] Php if($var) used to work
问题描述
我有一个网站的som代码,以前用得很好。我想检查是否设置了变量或会话。我使用了以下代码:
I have som code for a website, that used to work fine with. I want to check if a variable or session is set. I used the following code:
if ($_GET['something']){ //Do something }
现在我尝试在本地服务器上安装它,我得到一个错误代码,说有一个未定义的索引。
Now i try to install it on a local server, at i get a error code, saying that there is an undefined index.
它要求我这样做:
if (isset($_GET['']) { //Do something }
我可以快速解决这个问题,因为我不想更改代码那么多地方?
Can i make a quick fix for this, as i don't want to change the code so many places?
推荐答案
之前没有收到错误,因为你的error_reporting和/或display_error设置设置得过于宽泛。你的第一个片段 试图访问可能不存在的数组中的值。如果不是PHP 总是 发出通知。
只有在发出通知后才可以放心(通过关闭 display_errors
或设置 error_reporting
不报告通知( ~E_NOTICE
)。
You didn't get the error before, because your error_reporting and/or display_error settings were set too forgiving. Your first snippet is attempting to access a value in an array that might not exist. If it doesn't PHP always issues a notice.
It's only after the notice has been issued that it can be hushed up (by turning off display_errors
or setting error_reporting
to not report notices (~E_NOTICE
).
您要求快速修复:
if (isset($_GET['something']))
只要求你添加 isset()
。这很快,它解决了这个问题。此外:使用 isset
是你应该做的事情。总是。
only requires you to add isset()
. That's quick, and it fixes the issue. Besides: using isset
is what you should do. Always.
偏离主题,但也许有用:
正如我在评论中所解释的那样:现在最好的解决方法是解决问题本身,而不是忽略通知。为此,一个简单的脚本扫描你的项目目录中的.php文件(使用 glob
),读取它们并查找模式可能有用:
Off-topic, but perhaps useful:
As I've explained in the comments: the best course of action for you now is to fix the issue itself, not ignoring the notices. To that end, a simple script that scans your projects directories for .php files (using glob
), reads them in and looks for a pattern might prove useful:
foreach ($phpFiles as $file)
{
$code = file_get_contents($file);//read the file
if (preg_match_all('/if\s*\(\$[^[]+\[[^]]+\]\s*\)/',$code, $matches))
{//find all usages of "if ($rawVar['offset'])" in code
echo 'Expressions to check or fix in file: ', $file,':', PHP_EOL, '=>';
echo implode(PHP_EOL.'=>', $matches[0]), PHP_EOL;
}
}
运行脚本,或许将输出写入temp文件,并设置为工作。输出应如下所示:
run the script, perhaps write the output to a temp file, and set to work. The output should look something like:
Expressions to check or fix in file: scriptname.php:
=> if ($_GET['something'])
=> if ($var[123])
依此类推。这是一种可行的格式,并且给定时间,您也可以编写脚本来自动重构代码。
也许在这里使用更全面(如完整的)正则表达式喜欢:
And so on. It's a workable format, and given time, you might be able to write a script for you to automatically refactor the code, too.
Perhaps a more comprehensive (as in complete) regex to use here would be something like:
/if\s*\((\|\||&&|\()?\$[^[]+\[[^]]+\]\s*(\|\||&&|\))?/
但是,这也有一些警告,但这是一个开始。
But this, too, has some caveats still, but it's a start.
添加作业 - 我还会为你做一个好处:添加修复你在赋值表达式中遇到的问题的代码,比如 $ var = $ _GET ['something'];
。这可以自动完成,也非常容易:
Adding assignments - I'll do you one more favour: adding code that fixes the issues you get in assignment expressions like $var = $_GET['something'];
. This can be done automatically, and quite easily, too:
foreach ($phpFiles as $file)
{
$code = file_get_contents($file);//read the file
$clean = preg_replace(
'/(\$[^\s]+\s*={1}\s*)(\$[^[]+[^]]+\])\s*;/',
'$1isset($2) ? $2 : null;',
$code
);
file_put_contents($file, $clean);
}
我已经测试过这样的代码:
I've tested this code like so:
$code = '$foo = 123;
$foo = $_GET["bar"];';
$clean = preg_replace(
'/(\$[^\s]+\s*={1}\s*)(\$[^[]+[^]]+\])\s*;/',
'$1isset($2) ? $2 : null;',
$code
);
echo $clean, PHP_EOL;
它产生了预期的产出:
$foo = 123;
$foo = isset($_GET["bar"]) ? $_GET["bar"] : null;
结合这两个正则表达式,你应该很好地重构你拥有的代码......
Combine these two regex's and you should be well on your way to refactoring the code you have...
这篇关于Php if($ var)曾经工作过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!