Re: Undefined index....

[email protected] (Tim Streater)
Newsgroups php.general
Message-ID <[email protected]>
On 15 Mar 2013 at 13:10, Jay Blanchard <[email protected]> wrote: 

> I have inherited a mess of a home-grown PHP framework that literally
> fills the error_log with 100's of thousands of messages each day. First
> order of business was rotating the logs, now we are working through each
> error to solve them. This is a fairly basic error, but I for the life of
> me cannot remember what the solution is.
>
> I have a recursive function that reads from XML files and replaces xi:
> include directives. It looks like this -
>
> function includeXML($file, $locale, $url, $level = 1) {
>     // stuff
>     while(($line = fgets($fp)) !== false) {
>         if($pos === false) {
>             $xml .= $line;
>         } else {
>             includeXML('repository/' . $included, $locale, (int)$level
> + $pos - 1);
>         }
>     }
> }
>
> Why do I get the notice that $xml is an undefined index?

Because it's undefined. So is $pos. From what you've written above, both are local to includeXML. But neither is passed in as a parameter, nor is global. You can't initialise it within the function, it seems to me.

If $xml is supposed to be appended to and grown as you recurse up and down, then you have two choices:

1) Make them global
2) Pass both as extra parameters to includeXML

In both cases, each needs to be initialised before the first call to the recursive function

Solution (1)
============

$xml = '';
$pos = 0;    // Presumably.
includeXML ($file, $locale, $url, 1);

...

function includeXML ($file, $locale, $url, $level = 1) {

    global  $xml, $pos;

    // stuff
    while(($line = fgets($fp)) !== false) {
        if($pos === false) {
            $xml .= $line;
        } else {
            includeXML ('repository/' . $included, $locale, (int)$level
+ $pos - 1);
        }
    }
}

Solution (2)
============

$xml = '';
$pos = 0;    // Presumably.
includeXML ($xml, $pos, $file, $locale, $url, 1);

...

function includeXML (&$xml, $pos, $file, $locale, $url, $level = 1) {    // Note the & on the first parameter
    // stuff
    while(($line = fgets($fp)) !== false) {
        if($pos === false) {
            $xml .= $line;
        } else {
            includeXML ($xml, $pos, 'repository/' . $included, $locale, (int)$level
+ $pos - 1);
        }
    }
}


BTW it seems to me that you'll have the same problem with $included unless there's other code in includeXML that you've omitted.

--
Cheers  --  Tim
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.