php preg_replace_我需要定义$matches吗?
我通常使用php preg_replace_我需要定义$matches吗?,php,netbeans,pass-by-reference,Php,Netbeans,Pass By Reference,我通常使用$pattern,$subject,$matches,比如 preg_match_all("/\S/","words",$matches); 但是,我的IDE(Netbeans)正在对我大喊大叫,因为$matches(通过引用传递,并将被分配preg\u match\u all)的结果未定义,所以它不希望我将其作为参数传递。它喜欢这样的东西: $matches=[]; preg_match_all("/\S/","words",$matches); 但是,这似乎是多余的,php.n
$pattern
,$subject
,$matches
,比如
preg_match_all("/\S/","words",$matches);
但是,我的IDE(Netbeans)
正在对我大喊大叫,因为$matches
(通过引用传递,并将被分配preg\u match\u all
)的结果未定义,所以它不希望我将其作为参数传递。它喜欢这样的东西:
$matches=[];
preg_match_all("/\S/","words",$matches);
但是,这似乎是多余的,php.net上的示例没有提前定义变量。这样做的“正确”方式是什么?我可以忽略Netbeans吗?你可以安全地忽略Netbeans,或者你可以少抱怨 在使用之前声明变量可能被认为是一种好的风格,即使这些变量只是输出的。PHP不在乎:PHP将覆盖手头上的任何
$matches
,即使它不存在:
$matches = new StdClass();
$ok = preg_match_all('/foo/', "foo bar", $matches);
var_dump($ok, $matches);
无论这是好事还是坏事,都是用户定义的。:)
最后,您可以考虑一个助手函数,它既可以使NETBESE安静下来,又可以减少键入:
function get_matches($pattern, $subject, $options = null) {
$matches = array ();
preg_match_all($pattern, $subject, $matches, $options);
return $matches;
}
忽略NetBeans。正如您所说,PHP手册没有定义变量,我已经在所有错误报告中对其进行了测试,没有给出错误通知 此外,我使用的IDE PHPStorm不会显示
$matches
变量的未定义错误