I'm trying to create an array to store numbers and add them together.
<input type="number" name="numbers[]"/>
I'm getting an undefined variable on the following line
foreach($numbers as $number)
I'm sure this is probably something basic but I'm relatively new to php and help would be greatly appreciated.
If you posted the
input
s you're showing from one page to another and you need to run through the list you should set it up like this:If you want to get the sum of an array you dont need to loop you can use
array_sum
Example
Add some more code, but it basically means that $numbers is not declared yet at this point in the code.
Add this line before:
Now it shouldnt give you this error.
So now the question is, where is $numbers supposed to be set? For that we need more info and code.
I'm no PHP expert but if I understand you correctly and you are trying to put a variable in an array you would need to say what part of the Array I would assume. I'm pretty sure that
$array[i]
and$array
are not the same and that if you try to put something into$array
it will only be able to hold one thing at a time. If you are just gathering a bunch of numbers that someone enters from a form then you could do something like this.I hope that's right and I hope it helps.