Jump to content

How to get last value in for loop


t_machine

Recommended Posts

Thanks for the replies :) I will expand a little on my example.

In my example the "10" is defined but in my codes the number is determined by the results in a database. I am using the "for" loop to display the results and would like for it to reach the last result and echo some extra content.

 

Thanks

This is a nice way to do it :-P I prefer this method just to be my pure insane mind at work xD

 

<?php

for($i =0; $i<10; $i++){
}


echo $i;

?>

 

Tadaa! You just echoed the last item in the loop. It seems redundant, but works, for example:

 

<?php
while($row = mysql_fetch_array($query)){
$id = $row[id];
$username = $row[username];
$pass = md5($row[passwd]);
}

echo $id.$username.$pass;
?>

 

Quite literally, the loop keeps overwriting the variables (so to speak) so that when it finishes, the variable is stored with the last item in the loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.