3

I have a very simple task yet it keeps failing.

I want to have a function inside a class and the param that you pass into the function will be an array. All the function has to do is echo back the contents of the array.

Echoing out all the variables in for example the $_POST array is very easy to achieve with a foreach loop. However, it does not work in my class.

This was a very generic explanation. I you need more details please ask.

public function check_if_filled($array){ 
    foreach($array as $key->$value){ 
        echo $key . " : " . $value;
    } 
}

any advice why it gives me this error

Notice: Undefined variable: value in (my page) on line 117

Fatal error: Cannot access empty property in (my page) on line 117

8
  • Please post the class code that isn't working. Seeing the code is always easier then hearing the description of it. Commented Apr 24, 2011 at 3:15
  • Code, please. There's probably a trivial reason why the foreach is failing, and we need to see code to figure that out. Commented Apr 24, 2011 at 3:15
  • public function check_if_filled($array){ foreach($array as $key->$value){ } } Commented Apr 24, 2011 at 3:16
  • This is a very generic comment. A very generic answer is to post your code, and a generic action will be to vote to close this question Commented Apr 24, 2011 at 3:16
  • how do I post code with the formating Commented Apr 24, 2011 at 3:17

1 Answer 1

3

You used the wrong type of arrow in the foreach loop.

foreach($array as $key->$value)

should be

foreach($array as $key=>$value) 
Sign up to request clarification or add additional context in comments.

5 Comments

It's not an arrow, it's a hash rocket, Rocket.
@Rafe: What's a hash rocket? => is called T_DOUBLE_ARROW according to PHP. php.net/manual/en/tokens.php
@Rafe: -> is called the arrow operator in all languages.
@Rocket I call it the member dereference operator in C, and I don't think I've ever referred to it by name in PHP. Hash rocket is what Ruby programmers call it, which is funny in itself, plus your name is Rocket, so...
@Rafe: I've never heard it called a hash rocket. :-P

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.