Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP Notice: Undefined variable: array_var

Newbie
 
Join Date: Apr 2007
Posts: 6
#1: Apr 12 '07
I have a class with an empty array like so:

private $detail = array();


Then I have a function later on in the class:

function get_array_value()
{
$detail_info = ' ';
foreach($detail as $key=>$value)
{
$detail_info .= CreateDetailEntry($value);
}
}

when I create an instance of my class and try to use the function get_array_value, I get this:

PHP Notice: Undefined variable: detail in blah blah file line ??

Is there a problem with creating an empty array that gets filled in later?

Newbie
 
Join Date: Apr 2007
Posts: 6
#2: Apr 12 '07

re: PHP Notice: Undefined variable: array_var


Quote:

Originally Posted by dmain1971

I have a class with an empty array like so:

private $detail = array();


Then I have a function later on in the class:

function get_array_value()
{
$detail_info = ' ';
foreach($detail as $key=>$value)
{
$detail_info .= CreateDetailEntry($value);
}
}

when I create an instance of my class and try to use the function get_array_value, I get this:

PHP Notice: Undefined variable: detail in blah blah file line ??

Is there a problem with creating an empty array that gets filled in later?

--------------------------------------
I have tried declaring it public with the same result.
I tried creating it as private $detail = array(3); with the same result

What am I missing?
Newbie
 
Join Date: Apr 2007
Posts: 6
#3: Apr 12 '07

re: PHP Notice: Undefined variable: array_var


Quote:

Originally Posted by dmain1971

--------------------------------------
I have tried declaring it public with the same result.
I tried creating it as private $detail = array(3); with the same result

What am I missing?



changed it again: public $detail = array(1 => 'A');

same result. I have other arrays that work fine.
Newbie
 
Join Date: Apr 2007
Posts: 6
#4: Apr 12 '07

re: PHP Notice: Undefined variable: array_var


Quote:

Originally Posted by dmain1971

changed it again: public $detail = array(1 => 'A');

same result. I have other arrays that work fine.



Here is the rest of the error: "PHP Warning: Invalid argument supplied for foreach()", meaning the array I created.
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#5: Apr 13 '07

re: PHP Notice: Undefined variable: array_var


The $detail you are using in your function is local only to that function. To use a class variable you need to tell the class you are using the variable.
[PHP]$this->detail;[/PHP]
Reply