Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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 试图定义一个未定义的变量错误_Php - Fatal编程技术网

Php 试图定义一个未定义的变量错误

Php 试图定义一个未定义的变量错误,php,Php,我收到一个未定义的变量错误,我尝试添加var$nx=''但这没有帮助。我错过什么了吗注意:未定义变量:第55行/home/social/public_html/kernel/parser.php中的nx while (!feof($f)) { $s = chop(fgets($f,4096)); if ($s == '') continue;

我收到一个未定义的变量错误,我尝试添加
var$nx=''但这没有帮助。我错过什么了吗注意:未定义变量:第55行/home/social/public_html/kernel/parser.php中的nx

                while (!feof($f)) {
                    $s = chop(fgets($f,4096));
                        if ($s == '') continue;
                        if (substr($s,0,5) == '<!--[') {
                                if ($nx != '') $this->templ[] = $nx;
                                $this->templ[] = $s;
                                $nx = '';
                                }
                        elseif (substr($s,0,5) == '<?php') {
                                if ($nx != '') $this->templ[] = $nx;
                                $nx = $s;
                                }
                        else 
////// LINE 55                  $nx .= ($nx != '' ? "\n" : '').$s;

                        if (substr($nx,-2) == '?>') {
                                $this->templ[] = $nx;
                                $nx = '';
                                }
                        }
while(!feof($f)){
$s=印章(fgets($f,4096));
如果($s='')继续;

if(substr($s,0,5)=',因为您在if块中定义它,但假设它存在于else块中,即if与条件不匹配时使用什么

在此行之后初始化它,以便if、elseif和else都将变量初始化为空字符串(如您所需):

或者在整个街区之前

编辑:

但更好的解决方案是告诉$nx应该来自何处,因为您假设它存在于任何地方,并且它不同于空字符串。那么,它应该包含什么呢? 无论如何,请确保,无论它必须包含什么,它都是存在的。再次阅读您的代码时,我发现在许多地方,您希望它不是空字符串


你也没有提到你的代码是否是函数的一部分。如果是这样的话,请考虑这个函数有一个作用域,所以如果你在函数之外定义它,你需要把它加到那个函数中,否则函数就看不到它。

你在if块中定义了var。< /p> 这是正确的代码:

                $nx = '';
                while (!feof($f)) {
                $s = chop(fgets($f,4096));
                    if ($s == '') continue;
                    if (substr($s,0,5) == '<!--[') {
                            if ($nx != '') $this->templ[] = $nx;
                            $this->templ[] = $s;
                            $nx = '';
                            }
                    elseif (substr($s,0,5) == '<?php') {
                            if ($nx != '') $this->templ[] = $nx;
                            $nx = $s;
                            }
                    else 
                            $nx .= ($nx != '' ? "\n" : '').$s;

                    if (substr($nx,-2) == '?>') {
                            $this->templ[] = $nx;
                            $nx = '';
                            }
                    }
$nx='';
而(!feof($f)){
$s=印章(fgets($f,4096));
如果($s='')继续;

if(substr($s,0,5)==”位于定义了几个其他变量的文件顶部
                $nx = '';
                while (!feof($f)) {
                $s = chop(fgets($f,4096));
                    if ($s == '') continue;
                    if (substr($s,0,5) == '<!--[') {
                            if ($nx != '') $this->templ[] = $nx;
                            $this->templ[] = $s;
                            $nx = '';
                            }
                    elseif (substr($s,0,5) == '<?php') {
                            if ($nx != '') $this->templ[] = $nx;
                            $nx = $s;
                            }
                    else 
                            $nx .= ($nx != '' ? "\n" : '').$s;

                    if (substr($nx,-2) == '?>') {
                            $this->templ[] = $nx;
                            $nx = '';
                            }
                    }