0

I get these numbers seperated by commas from a textarea but I get an error when I try to loop through them. How do I do it? This is my code:

$numbers = $_GET['numbers'];

foreach($numbers as $number){
echo $number;
}
1
  • Are you sure $_GET['numbers'] is an array? Apparently it is not. Do var_dump($_GET['numbers']) and see. Commented Apr 15, 2014 at 14:59

2 Answers 2

6

You should first make an array out of $numbers. You can do this by adding this line:

$numbers = explode(',', $_GET['numbers']);

Then, before you use them in the foreach loop you should use trim() to remove whitespace from the start and end:

foreach($numbers as $number){
    $number = trim($number);

    echo $number
}
Sign up to request clarification or add additional context in comments.

Comments

3

If $_GET['numbers'] is a comma-separated list, it's not an array.

foreach(explode(",",$_GET['numbers']) as $number)

Comments

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.