0

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 = '';
                                }
                        }
1
  • at the top of the file where a few other var's are defined Commented Jan 14, 2012 at 9:46

2 Answers 2

2

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 = '';
                            }
                    }
Sign up to request clarification or add additional context in comments.

Comments

0

Because you're defining it in the if block, but assuming it exists in the else block, i.e. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.