I want to create an array of numbers: 10, 9,8...to 1. But when I echo $numbers, I get "Array" as the output as to the numbers.
There is probably a simple thing I missed, can you please tell me. thanks!
$numbers=array();
for ($i=10; $i>0; $i--){
array_push($numbers, $i);
}
echo $numbers;
No you are not missing anything. Of course $numbers
is an array.
If you do print_r($numbers)
than you see what elements are in the array.
It very much depends what you want to do with array in the end. You can for example also loop over the array values:
foreach($numbers as $number) {
//whatever you want to do
echo $number;
}
If you only want to print theses 10 numbers you can also just do:
for ($i=10; $i>0; $i--){
echo $i;
}
As I said it depends on what you want to do :)
Depends on what you want the output to look like.
For debugging purposes, this should work:
print_r($numbers);
For a "prettier" output:
foreach ($numbers as $key => $value)
echo $key . "=" . $value
First of all, you can create that array much easier using range()
$numbers = range( 10, 1 );
Secondly, only scalars can be echoed directly - complex values like objects and arrays are casted to strings prior to this. The string cast of an array is always "Array".
If you want to see the values, you have to flatten the array somehow, to which you have several options - implode() might be what you're looking for.
echo implode( ',', $numbers );
For debugging purposes you might like:
echo '<pre>' . print_r($numbers, true) . '</pre>';
As it's output is clear without having to look at page source.