简体   繁体   中英

trying to define an, Undefined Variable error

I'm receiving an Undefined variable error, I tried adding var $nx = ''; but that didn't help. Am I missing something? Notice: Undefined variable: nx in /home/social/public_html/kernel/parser.php on line 55

                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 = '';
                                }
                        }

You are defined var in the if block.

This is right code:

                $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 = '';
                            }
                    }

Because you're defining it in the if block, but assuming it exists in the else block, ie what is used when the if don't match the condition!

Initialize it after this line, so that if,elseif and else all have the variable initialize as empty string (as you wanted it):

$s = chop(fgets($f,4096));
$nx = '';

or before the whole block altogether

EDIT:

But a better solution would be to tell what that $nx should come from, as you are assuming everywhere it exists and it is different from an empty string. So, what is it supposed to hold? Anyway, make sure, whatever it must contain, it exists. Reading your code a second time, i see in many places you want it not to be an empty string..

Also, you didn't mention if your code is part of a function or not. If this being the case, consider that function have a scope, so if you define it outside the function you need to feed it to that or the function won't see it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM