PHP Notice: Undefined variable: array_var

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmain1971
    New Member
    • Apr 2007
    • 6

    #1

    PHP Notice: Undefined variable: array_var

    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 .= CreateDetailEnt ry($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?
  • dmain1971
    New Member
    • Apr 2007
    • 6

    #2
    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 .= CreateDetailEnt ry($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?

    Comment

    • dmain1971
      New Member
      • Apr 2007
      • 6

      #3
      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.

      Comment

      • dmain1971
        New Member
        • Apr 2007
        • 6

        #4
        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.

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          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]

          Comment

          Working...