Timothy Fisher
Timothy Fisher

Reputation: 1119

PHP - Explain this while/for loop to me

I'm new to PHP and I am stuck trying to understand this basic while/for loop. There's a couple of things I just wanted to clarify. Here is the code:

while ($row = $sth->fetch (PDO::FETCH_NUM))
{
  print ("<tr>\n");
  for ($i = 0; $i < $sth->columnCount (); $i++)
   {
     print ("<td>" . htmlspecialchars ($row[$i]) . "</td>\n");
   }
   print ("</tr>\n");
}

I know the while is evaluating for true, so it's getting the result set of the query, and then printing the tags.

However I'm stuck on the

for ($i = 0; $i < $sth->columnCount (); $i++)

I know that the first expression evaluates once, the second evaluates at the beginning and only moves on if it's true, and the third increments by +1. But what exactly is this doing in literal terms and why is it "columnCount"?

Upvotes: 3

Views: 117

Answers (4)

Guillermo Maschwitz
Guillermo Maschwitz

Reputation: 1106

What that line does is making it possible to iterate on each column of the specific $row. $sth->columnCount() returns the number of columns for what seems to be a table data structure.

On the other hand, the for loop will execute the print statement i times, where i will be 0 when starting and will be increasing itself to be as the number of columns returned by $sth->columnCount() minus 1.

Why minus 1?

Because usually in programming you start counting from 0 instead of 1.

So $row[0] represents the 1st column of your row, $row[1] represents the 2nd column, and so on, until you reach the last column which is $row[$sth->columnCount() - 1]

Upvotes: 1

John Green
John Green

Reputation: 13435

This is a PDO database setup.

In literal terms, the $row is a row fetched from the database, from code that lives above in a statement which didn't make your cut. It then sends an iterator into every field of the row (getting its number from the columnCount function(http://php.net/manual/en/pdostatement.columncount.php), and prints out some html.

Generally, this kind of iterator is 'less-than-ideal' code for several reasons. Generally, coding practices in PHP prefer to use foreach instead of a for iterator unless one actually needs the count as we pass through, ostensibly why the fetch is done as FETCH_NUM, although it is never used.

while ($row = $sth->fetch())
{
  print ("<tr>\n");
  foreach ($row as $key=>$value)
   {
     print ("<td>" . htmlspecialchars($value) . "</td>\n");
   }
   print ("</tr>\n");
}

Upvotes: 1

blacksheep_2011
blacksheep_2011

Reputation: 1143

this will iterate through all fields of one row.

columnCount()returns the number of fields in your statement.

So if your statement is:

SELECT a,b,c,d FROM test;

columnCount() return 4.

Upvotes: 0

CodyEngel
CodyEngel

Reputation: 1501

The for loop is starting at the beginning of the dataset and evaluating until it reaches the end of the dataset.

The dataset will always start at 0, until $i is no longer less than the value of columnCount() it will continue.

$sth is some instantiated object (variable) which has a method called columnCount(). That method likely returns the total number of columns which $sth is representing.

It may also help to say that this for loop can be rewritten as a while loop:

$i = 0;
while($i < $sth->columnCount()) {
    // do something
    $i++;
}

Upvotes: 0

Related Questions