Re: Undefined index....

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 15 Mar 2013 at 13:10, Jay Blanchard <jay.blanchard@xxxxxxxxxxxxxxxxxxx> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux