|
|
 |
Los bucles for son los bucles más
complejos en PHP. Se comportan como su contrapartida en C. La
sintaxis de un bucle for es:
La primera expresión (expr1) se
evalúa (ejecuta) incondicionalmente una vez al principio del
bucle.
Al comienzo de cada iteración, se evalúa
expr2 . Si se evalúa como
TRUE, el bucle continúa y las sentencias anidadas se
ejecutan. Si se evalúa como FALSE, la ejecución del
bucle finaliza.
Al final de cada iteración, se evalúa (ejecuta)
expr3.
Cada una de las expresiones puede estar vacÃa. Que
expr2 esté vacÃa significa
que el bucle deberÃa correr indefinidamente (PHP
implicitamente lo considera como TRUE, al igual que C). Esto
puede que no sea tan inútil como se podrÃa pensar,
puesto que a menudo se quiere salir de un bucle usando una
sentencia break
condicional en vez de usar la condición de
for.
Considera los siguientes ejemplos. Todos ellos muestran números del
1 al 10:
Por supuesto, el primer ejemplo parece ser el mas elegante (o
quizás el cuarto), pero uno puede descubrir que ser capaz de
usar expresiones vacÃas en bucles for
resulta útil en muchas ocasiones.
PHP también soporta la "sintaxis de dos puntos" alternativa
para bucles for.
Otros lenguajes poseen una sentencia foreach
para traducir un array o una tabla hash. PHP3 no posee tal
construcción; PHP4 sà (ver foreach). En PHP3, se
puede combinar while con las funciones
list() y each() para
conseguir el mismo efecto. Mirar la documentación de estas
funciones para ver un ejemplo.
frank at booksku dot com
10-Aug-2006 11:15
re: jdtoth:
I'm fairly sure the following gives you exactly the output you're looking for:
$arr = array(12,18,24,36);
foreach($arr as $num)
foreach($arr as $inner_num)
foreach($arr as $last_num)
echo "$num-$inner_num-$last_num\n";
thomas et luegger _dot_ de
17-Jul-2006 12:44
The previous post from Fatalerror is not correct. Variables enjoy no special treatment inside 'for' statements. At least I was not able to reproduce such a behavior. The code below shows, that the value of $a from loop 1 is still valid outside the scope of it's loop. It can be used without further initialisation for loop 2. So 'while' and 'for' loops are just a matter of taste.
<?php
echo ("In loop 1: ");
for ($a = 0; $a < 3; $a++) echo("$a,");
echo ("\r\nAfter loop 1: a = $a");
echo ("\r\n\r\nIn loop 2: ");
for ( ; $a++ < 20; )
{
echo ("$a,");
if ($a==10) break;
}
echo ("\r\nAfter Loop 2: a = $a");
?>
output:
In loop 1: 0,1,2,
After loop 1: a = 3
In loop 2: 4,5,6,7,8,9,10,
After Loop 2: a = 10
Greetings, Tom
FatalError
28-Dec-2005 11:56
To add to my previous note, I found a big difference between the two statements. With a "while" statement, you can use $a (from my examples) outside the "while" statement. However, with the "for" statement, you can only use $a within the scope of that statement. In other words, you can't use $a after that "for" statement and expect it to still be equal to the same number it was before.
FatalError
27-Dec-2005 09:01
You can also use a "while" loop to get the same results as a "for" statement. These two examples are basically the same thing:
<?php
for ($a = 0; $a < $somevar; $a++) {
}
?>
<?php
$a = 0;
while ($a < $somevar) {
$a++
}
?>
see_the_address at the_message dot text ;)
26-Nov-2005 06:29
Here a bit more flexible solution of a task "brutforce" by "jdtoth -at- gmail dot com", and also should be faster ;-) It uses recursion. But it requires quite much memory (especially when playing with $num_positions), I'm not sure it's OK...
If anybody has ideas about that - you are welcome: ultraBoy aat mail ddot ru
<?php
function get_result($len, &$alphabet) {
if ($len == 1) {
$result = array();
while (list($row_key, $row) = each($alphabet)) {
$result[] = array($row);
};
}
else {
$result = get_result($len - 1, $alphabet);
reset($result);
$new_result = array();
while (list($row_key, $row) = each($result)) {
reset($alphabet);
while (list($l_key, $l) = each($alphabet)) {
$new_row = $row;
$new_row[] = $l;
$new_result[] = $new_row;
};
}
$result = $new_result;
}
return $result;
}
$numbers = array(12,18,24,36);
$num_positions = 3;
echo 'Total rows: '.pow(count($numbers), $num_positions).'<br />';
$result = get_result($num_positions, $numbers);
foreach ($result as $row_num => $row) {
echo "$row_num: ";
foreach ($row as $val) {
echo $val.' ';
}
echo '<br />';
}
?>
jdtoth -at- gmail dot com
25-Nov-2005 03:35
I was fooling around, trying to figure out the combination to a lock that I couldnt remember the combo to. I did remember it involved a few numbers, so I wrote the following:
<?php
$numbers = array (12,18,24,36);
for ($i=0; $i <= 10000; $i++)
{
srand ((double) microtime() * 1000000);
$random1 = rand(0,3);
srand ((double) microtime() * 1000000);
$random2 = rand(0,3);
srand ((double) microtime() * 1000000);
$random3 = rand(0,3);
$results[] = $numbers[$random1].' - '.$numbers[$random2].' - '.$numbers[$random3];
}
$results = array_unique($results);
echo "** Unique Results ".count($results)." ** <br/><br/>";
foreach ($results as $output) echo $output . "<br />";
?>
What would be the proper way of writing this code? Obviously my 'brute-force' method of running a random sequence an extreme amount of times does work and yields what i think is the correct number of combinations, but there must be a more efficient method?
Maciek
15-Nov-2005 11:12
in reply to mbvlist about his loop:
<?php
for($i=0; $i < $max; $i++ && print 'increment,')
{
do_something();
echo " i = $i\n";
}
echo "finally, i = $i\n";
?>
Your loop works as you observed, because php evaluates the following as expression:
($i++ && print 'increment)
When $i=0, $i++ returns false, so the second part of the expression (print 'increment') is not evaluated, since there is no output. Since $i > 0 returns true, your print will be evaluated and you will see output..
13-Nov-2005 03:23
if you wanted to make a next previous thing for mysql this would be it
$page = $_GET['page'];
// mysql query goes here
$limit = 30; //gets 30 of whatever
if(!$page){
$page = "1";
}
$limitvalue = $page * $limit - ($limit); //find the limit value
$query = mysql_query("SELECT * FROM `table`ORDER BY id DESC LIMIT $limitvalue, $limit");
$numrows = mysql_query("SELECT * FROM `forum_topics` WHERE `forumid` = '$fid'");
$numofpages = ceil($numrows / $limit);
while ($r = mysql_fetch_array($query)){
// your stuff goes here
}
$pageprev = $page-1;
$pagenext = $page+1;
if ($page != "1"){
echo " <a href='http://indices.com.es/linkhere.html$pageprev'><< Previous</a> ";
}
for ($i = 1; $i <= $numofpages; $i++) {
if ($i == $page){
echo "<b>$i</b> ";
}
else {
echo "<a href='http://indices.com.es/linkhere.html$i'>$i</a> ";
}
}
if ($numrows > $limit){
if ($page != $numofpages){
echo " <a href='http://indices.com.es/linkhere.html$pagenext'>Next >></a> ";
}
}
There is also a few other ways but this is how i do it ^^
mbvlist at fastmail dot fm
08-Nov-2005 12:37
While fuzzeling around with iterators (trying to find the next and previous item in a list), discovered something odd:
<?php
for($i=0; $i < $max; $i++ && print 'increment,')
{
do_something();
echo " i = $i\n";
}
echo "finally, i = $i\n";
?>
If $max is set at 1 (only the first run will occur), it works as I expected:
i = 0
finally, i = 0
But with $max at a higher value (say 2) it works different:
i = 0
increment, i = 1
increment, finally, i = 2
After the loop was finished $i was incremented! As I read right now, this is what should happen. But it doesn't occur in the first case. As I said, I was searching something using an iterator, and after the loop I wanted to know the next value. Because of this 'feature' (a.k.a. bug) that didn't work.
Be warned!
robert at extremedesigns dot de
06-Aug-2005 04:58
Here is a quick function I wrote for creating a web-true colour palette.
function webTrueColors() {
// create array for hexadecimal colors
$colors = array("00", "33", "66", "99", "CC", "FF");
// build table
$c= "<table cellspacing=0 cellpaddding=0 border=1>";
// loop through all combinations
for ($i=0; $i<6; $i++) {
for ($j=0; $j<6; $j++) {
$c.= "<tr>";
for ($k=0; $k<6; $k++) {
$thisColor = $colors[$i].$colors[$j].$colors[$k];
// display color and hex value
$c.= "<td bgcolor=\"#".$thisColor."\">".$thisColor."</td>";
}
$c.= "</tr>";
}
}
// close table
$c.= "</table>";
// output to screen
echo $c;
}
JustinB at harvest dot org
04-Aug-2005 04:23
For those who are having issues with needing to evaluate multiple items in expression two, please note that it cannot be chained like expressions one and three can. Although many have stated this fact, most have not stated that there is still a way to do this:
<?php
for($i = 0, $x = $nums['x_val'], $n = 15; ($i < 23 && $number != 24); $i++, $x + 5;) {
}
?>
dre
21-Jul-2005 11:01
In reply to M. Berndt's code sample: recursion is not "always" faster, especially not when you consider how big the stack's getting.
With FAKTOR = 50:
for(): 0.0222418308258
recursion: 0.175624132156
With FAKTOR = 1000:
for(): 0.228713035583
recursion: 0.459243059158
pmv at nipl dot net
04-Jul-2005 12:55
Actually, it seems calling echo inside a for expression is not allowed, at least as of PHP 5.0.4 (built: May 10 2005 09:56:44). Executing the said code results in the following error:
Parse error: parse error, unexpected T_ECHO in /usr/local/iwcdns/webadmin/foo.php on line 2
However, using the print() function seems to work. From this, it seems sensible to think that PHP does not treat echo as a expression. The following will work as expected:
<?php
for ($i = 'a'; $i != "aa"; $i++, print $i);
?>
(Note the use of the character notation 'a', instead of just a. Using the latter works, but is wrong, as explained in section "Why is $foo[bar] wrong?" of the array section of the manual (http://www.php.net/manual/en/language.types.array.php).)
Also, bpgordon's further shortening is not exactly equivalent to mison's examples. In a for loop, the step expression (i.e. the third one) is executed _after_ the body of the loop. Therefore, a more accurate equivalent would be:
<?php
for ($i = 'a'; $i != "aa"; print $i, $i++);
?>
And further shortening this:
<?php
for ($i = 'a'; $i != "aa"; print $i++);
?>
bpgordon at gmail dot com
12-Jun-2005 04:31
Mison's example can be shortened even further:
<?php
for ($i = a; $i!="aa"; $i++, echo $i)
?>
mison
27-May-2005 02:31
In a previous note:
Following moniarde at yahoo dot com 's idea
<?php
for ($i = a; $i!="aa" ; $i++) {
echo $i;
}
?>
This can be trimmed slightly further to:
<?php
for ($i = a; $i!="aa" ; $i++) echo $i;
?>
15-Jan-2005 04:57
Following moniarde at yahoo dot com 's idea
<?php
for ($i = a; $i!="aa" ; $i++) {
echo $i;
}
?>
will be good.
fb_mathman at hotmail dot com
01-Nov-2004 11:20
It is intersting how to use a structure that can substitue "for".
In the next lines, for instance, I create a function that get the smallest free_id for a table. It has good applications for the phpBB forum.
<?php
function assign_free_id($table, $id_field)
{
global $db;
$sql = "SELECT $id_field FROM $table WHERE 1 ORDER BY $id_field ASC";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Couldn't get id parameters for all the elements of the table", "", __LINE__, __FILE__, $sql);
}
while ($arr = $db->sql_fetchrow($result))
{
$row[]=$arr;
}
$max_elements = count($row);
if ($max_elements==0)
{
$id= 1;
return ($id);
}
$i=1;
for ( $j=0; $j<$max_elements; $j++)
{
if ($i==intval($row[$j][0]))
{
$i++;
}
else
{
$id=$i;
return $id;
}
}
}
?>
jphansen at uga dot edu
15-Oct-2004 12:31
as harbater at teamgenesis dot com noted, don't use a comma to separate conditions in expr2. But if you do, the result will be ONLY the last condition being evaluated for the continuation of the loop. So this would iterate infinitely: for(; false, true;); and if you swapped the true and false it would break immediately.
I wouldn't count using a comma completely out of the question, since there may be a rare circumstance where you would only want to break if the last evaluation returns false.
moniarde at yahoo dot com
31-May-2004 09:06
As previously mentioned,
<?
for ($i = a; $i <= z; $i++) {
echo $i
}
?>
will print a -> z, then aa, ba, ca , etc -> yz. However, doing this:
<?
for ($i = a; ; $i++) {
echo $i;
if ($i == "z") {
break;
}
}
?>
will print only a->z. It's simple, but it works.
user at host dot com
19-Apr-2004 03:53
Also acceptable:
for($letter = ord('a'); $letter <= ord('z'); $letter++)
print chr($letter);
hayes029 at bama dot ua dot edu
05-Feb-2004 11:16
For the purposes of outputing the alphabet,
<?php
for ($i="A"; $i <= "Z"; $i++) echo "$i<br>";
?>
will actually output A-Z, then begin again with AA, AB, etc. until it finishes with YZ. Similarly,
<?php
for ($i="A"; $i <= "ZZ"; $i++) echo "$i<br>";
?>
will output A-Z, AA, AB... ZZ, then begin again with AAA, AAB, AAC... up to ZYZ.
Thus an alternative to timrosseel's example for outputting the alphabet is:
<?php
for ($i="A"; $i != "AA"; $i++) echo "$i<br>";
?>
php at project2501 dot plus dot com
15-Sep-2003 04:45
just to clarify that you can 'chain' expr1, expr2 and expr3, for instance:
<?
for($i=0 , $j=10 ; $i<5 ; $i++ , $j+=5, $k=($i+$j))
{
echo "i: $i<br />j: $j<br />k: $k<br /><br />";
}
?>
produces:
i: 0
j: 10
k:
i: 1
j: 15
k: 16
i: 2
j: 20
k: 22
i: 3
j: 25
k: 28
i: 4
j: 30
k: 34
in case you need to do more than one operation per iteration.
The reason $k does not start with a value of 10, is because expr3 ($k=($i+$j)) is evaluated at the -end- of each iteration.
bishop
17-Jul-2003 04:43
If you're into 1-liners or compact code:
<?php
implode("\n", range('a','z'));
?>
Run-times (on my boxes) are comparable to the other methods using for(). This version is (arguably) more readable than other solutions.
bishop
17-Jul-2003 01:23
If you're already using the fastest algorithms you can find (on the order of O(1), O(n), or O(n log n)), and you're still worried about loop speed, unroll your loops using e.g., Duff's Device:
<?php
$n = $ITERATIONS % 8;
while ($n--) $val++;
$n = (int)($ITERATIONS / 8);
while ($n--) {
$val++;
$val++;
$val++;
$val++;
$val++;
$val++;
$val++;
$val++;
}
?>
(This is a modified form of Duff's original device, because PHP doesn't understand the original's egregious syntax.)
That's algorithmically equivalent to the common form:
<?php
for ($i = 0; $i < $ITERATIONS; $i++) {
$val++;
}
?>
$val++ can be whatever operation you need to perform ITERATIONS number of times.
On my box, with no users, average run time across 100 samples with ITERATIONS = 10000000 (10 million) is:
Duff version: 7.9857 s
Obvious version: 27.608 s
thewhiteness at hotmail dot com
16-Dec-2002 02:10
Regarding ben's note: The reason for this is that your first example uses neither the $i++ or $i+= operators. It's like telling PHP to add nothing to it ($i+).
Another way to possibly speed a for loop would be this:
...
for (;condition;) {
// your statement/block here
}
...
This way, if you have a variable that is being updated in the body of the loop anyways, you can use it, rather than specify a number of iterations.
nzamani at cyberworldz dot de
17-Jun-2001 11:47
The point about the speed in loops is, that the middle and the last expression are executed EVERY time it loops.
So you should try to take everything that doesn't change out of the loop.
Often you use a function to check the maximum of times it should loop. Like here:
for ($i = 0; $i <= somewhat_calcMax(); $i++) {
somewhat_doSomethingWith($i);
}
Faster would be:
$maxI = somewhat_calcMax();
for ($i = 0; $i <= $maxI; $i++) {
somewhat_doSomethingWith($i);
}
And here a little trick:
$maxI = somewhat_calcMax();
for ($i = 0; $i <= $maxI; somewhat_doSomethingWith($i++)) ;
The $i gets changed after the copy for the function (post-increment).
I don't know if
for ($i = 0; $i <= $maxI; somewhat_doSomethingWith($i), $i++) ;
is correct...at least it's not really clear.
harbater at teamgenesis dot com
27-Mar-2001 06:55
If you want two conditions to be evaluated in expr2, you can't just separate them with commas like you can with expr1 and expr3.
For example, this does not work:
for ($i=$start, $count=0; $i<$total, $count<10; $i+=10, $count++)
But this does:
for ($i=$start, $count=0; ($i<$total && $count<10); $i+=10, $count++)
| |
| | Citas célebres | Una severa autocrítica constituye el más precioso don del pensador: ¡Nada de embriagarse con el propio vino, sea bueno o malo! Santiago Ramón y Cajal Médico español (1852-1934) | | Citas en tu mail | | ©Contenidos Gratis |
| Chiste de... Animales | | La mofeta | Era un matrimonio que estaba de vacaciones en Canadá, y la mujer se encaprichó de una mofeta. Total, que se la llevó a la habitación.
Cuando llegó la hora de volver, el marido ya no sabía qué decirle para que la dejase, y al final se le ocurrió la excusa de la aduana.
- María, si te la ven en la aduana te acusarán de tráfico de animales.
- No te preocupes, le dice la mujer, me la escondo en las bragas y ya está.
- Pero cariño, con lo que huele.....
- Es igual ¡¡¡ QUE SE JOROBE!!!! | | Chistes en tu mail | | ©ContenidosGratis |
| Inicio | Acción | Estrategia | Palabras | Puzzles | Solitarios | Foro Trucos |  | Cake Mania Jugadores: 6835 Categoría del juego: Acción Objetivo del juego: Ayuda a Jill a recuperar la pastelería de su abuela llevando su propia pastelería; consigue clientes y gana dinero. |
|  | Rainbow Web Jugadores: 2199 Categoría del juego: Puzzles Objetivo del juego: Rompe un pegajoso hechizo y salva un reino de fantasía en Rainbow Web. Tendrás toneladas de diversión mientras juegas a este mágico desafío para la mente. |
|  | Mahjongg Fortuna Jugadores: 12462 Categoría del juego: Solitarios Objetivo del juego: Velocidad y habilidad mental son las armas más importantes en esta versión de un antiguo juego asiático. Despeja el tablero lo antes posible haciendo clic en las fichas iguales y gánate la fama eterna de la puntuación más alta. |
|  | Chainz 2 Jugadores: 6955 Categoría del juego: Puzzles Objetivo del juego: Entra en el mundo de las combinaciones con Chainz 2: Relinked, emocionante secuela del exitazo del año pasado, Chainz. Gira eslabones y crea combinaciones de 3 ó más. |
|  | Delicious Jugadores: 4405 Categoría del juego: Acción Objetivo del juego: ¿Eres un as de la multitarea? ¿Quieres que tus clientes estén contentos? ¡Pues Delicious es tu juego! Sacia el apetito de los clientes y tenlos contentos; ¡no te arriesgues! |
|  | Bookworm Jugadores: 4568 Categoría del juego: Palabras Objetivo del juego: Junta las letras para formar palabras. ¡Las palabras más largas valen más puntos! |
|  | Zuma Jugadores: 4976 Categoría del juego: Acción Objetivo del juego: Controla el ídolo de la rana de piedra de los antiguos Zuma en este intrigante enigma de acción. ¡Dispara bolas para formar conjuntos de tres, pero si dejas que lleguen a la calavera dorada morirás! |
|  | Jewel of Atlantis Jugadores: 3798 Categoría del juego: Puzzles Objetivo del juego: Descubre la ciudad hundida de la Atlántida y busca valiosos tesoros. Viaja más allá de las profundidades del mar y vive trepidantes aventuras en Jewel of Atlantis. |
|  | Jewel Quest Jugadores: 3727 Categoría del juego: Puzzles Objetivo del juego: Convierte la arena de la antigua selva en oro tan rápido como puedas juntando grupos de 3 elementos. ¡Los grupos más grandes valen más puntos! |
|  | Bejeweled 2 Jugadores: 3659 Categoría del juego: Puzzles Objetivo del juego: Con cuatro modos de juego únicos y fascinantes, nuevas piezas de juego explosivas e imponentes fondos planetarios, Bejeweled 2 es mucho más adictivo que nunca. |
|
|