I am facing a Peculiar behavior or you can say ghostly attitude of for loop in php. I am retrieving 10000 products using an API. After that I am inserting those products in my Database.Codes are working properly in localhost but creating problem in production server.I am describing it below.
$value_array = array();
$counter=0;
$time_start = microtime(true);
for($j=0;$j<count($result);$j++)
{
if(isset($result->Item[$j]->itemID))
{
$value_array[] = (int)$result->Item[$j]->itemID.','.(int)$result->Item[$j]->systemSku;
$counter++;
}
else
{
var_dump($result);
die();
}
if($counter == 100)
{
echo $counter;
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo '<br /><b>Total Execution Time:</b> '.$execution_time.' Sec';
die();
}
}
Above code create output like below
100
Total Execution Time: 39.075633049011 Sec
But if I change the code like below, I am getting a white page (No output).
$value_array = array();
$counter=0;
$time_start = microtime(true);
for($j=0;$j<count($result);$j++)
{
if(isset($result->Item[$j]->itemID))
{
$value_array[] = (int)$result->Item[$j]->itemID.','.(int)$result->Item[$j]->systemSku;
$counter++;
}
else
{
var_dump($result);
die();
}
if($counter == 9000) //I changed the code here
{
echo $counter;
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo '<br /><b>Total Execution Time:</b> '.$execution_time.' Sec';
die();
}
}
I changed the code like below again.
$value_array = array();
$counter=0;
$time_start = microtime(true);
for($j=0;$j<count($result);$j++)
{
if(isset($result->Item[$j]->itemID))
{
$value_array[] = (int)$result->Item[$j]->itemID.','.(int)$result->Item[$j]->systemSku;
$counter++;
}
else
{
var_dump($result);
die();
}
echo $counter.'<br/>'; //I changed the code here
if($counter == 9000)
{
echo $counter;
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo '<br /><b>Total Execution Time:</b> '.$execution_time.' Sec';
die();
}
}
I am getting output like below
all counter values
9000
Total Execution Time: 562.96646595001 Sec
What can be the problem ?? Why the first code behaving weird ??