Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php preg_replace_我需要定义$matches吗?_Php_Netbeans_Pass By Reference - Fatal编程技术网

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
变量的未定义错误