0

Summary: Trying to add +1 each time I go through my foreach loop, but it keeps going up even though I would only expect a maximum of 11 results in the loop

I tried to move around on the $i++; but without luck, I also check that I made sure the $i = 0; was outside of the foreach loop.

<?php
$facade = new NewsArticleFacade();
$news = $facade->getAll();
unset($facade);

$i = 0;
foreach($news as $new)
{
  $i++;
  if ($i = 5000000000000000)
  {
    echo "if statement: " . $i;
    break;
  }
  $content = substr($new->getContent(), 0, 69);
  echo "<div class='news'>";
    echo "<h4><a href='/news/news.php?id=".$new->getNewsID()."'>".$new->getTitle()."</a></h4>";
    echo "<span>by <a href='#'>".$new->getUser()->getUsername()."</a> - ".$new->getDate()->format("d-M-Y")."</span>";
    echo "<p>".$content." ...</p>";
    echo "<div class='gradient'></div>";
    echo "<img src='img/news/0000000001.jpg'>";
  echo "</div>";
}
?>

What really confuses me is if I remove the if statement for the loop, and just echo $i each time, it will echo out 11 in total, what am i doing wrong with my if?

I would expect the loop to go through 11 times since I have 11 entries in the database for this selection, but it keeps going up, if I echo within the if statement i can see the value will just be whatever i limit the if statement to be.

Thanks in advance!

Yoav Kadosh
  • 4,807
  • 4
  • 39
  • 56
pkj
  • 109
  • 1
  • 9
  • `if ($i = 5000000000000000)` here you assign instead of compare `==` – Banzay Jun 01 '19 at 19:44
  • So what is `NewsArticleFacade`? Since it apparently returns an iterator that doesn't stop delivering items - does it return false instead when there are no more news to return? Does `while($new = next($news)) { ... ` work better? – MatsLindh Jun 01 '19 at 19:46

1 Answers1

0

This

if ($i = 5000000000000000)

Is assignment and it makes little sense as such in your code. You probably wanted to have comparison operation which is expressed with ==

if ($i == 5000000000000000) 
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141