I have the following php code where it goes through a loop and push elements to an array. I wanted to count elements in that array at the end of the loop. But it is not showing anything. Can someone please help me here?
<?php
$i=0;
$uphPerOperator = [];
while($i<10){
$uphPerOperator = array_push($uphPerOperator,$i);
$i++;
}
$uphArrayCount = count($uphPerOperator);
echo $uphArrayCount; ?>
1 Answer 1
I got the code working by changing it following way
<?php
$i=0;
$uphPerOperator = [];
while($i<10){
array_push($uphPerOperator,$i);
$i++;
}
$uphArrayCount = count($uphPerOperator);
echo $uphArrayCount; ?>
Basically I changed $uphPerOperator = array_push($uphPerOperator,$i); to array_push($uphPerOperator,$i); and it works
answered Sep 13, 2019 at 3:14
Anu
1,1392 gold badges21 silver badges51 bronze badges
Sign up to request clarification or add additional context in comments.
lang-php
array_pushthe wrong way, see the php manual to see how this function works and what it is supposed to return.