[FX.php List] A nagging question: Foreach

Leo R. Lundgren leo at finalresort.org
Mon Sep 22 16:59:41 MDT 2008


If you have only one statement that should be run in the loop, you  
can do simply:

	foreach(..)
		statement;

But if you have two or more statements that should be run in the  
loop, you must enclose them either with { and } or the foreach ..  
endforeach;. How else can PHP tell what following lines you want to  
be part of the loop?

So for example:

	foreach(..) {
		statement;
		statement;
	}

	foreach(..):
		statement;
		statement;
	endforeach;

Note the lack of a colon in the first example, the use of {} in the  
second example, and the use of a colon and a semicolon in the third  
example.

Check the PHP manual under "Control Structures" for more information.


23 sep 2008 kl. 00.48 skrev Jonathan Schwartz:

> Hi Folks,
>
> A long term nagging question:
>
> Foreach syntax after an Instance
> After an instance of FMPFind, FMNew, FMEdit, etc, we all use the  
> standard foreach statement to retrieve values from a single record  
> that was just created, edited, etc.  Sometime, I leave the foreach  
> statement in the same place and retrieve the values later in the  
> script.  When using the foreach statement in this second case, I  
> find that I have to leave an empty line after the foreach.   
> Otherwise, things go wrong.
>
>
> THIS IS FINE:
> $findResult = $query->FMFind();
> foreach($findResult['data'] as $key => $value)
> $retrievedvalue = $value['retrievedvalue'][0];
>
> THIS ISN'T:
> $findResult = $query->FMFind();
> foreach($findResult['data'] as $key => $value)
> (SOME OTHER STATEMENTS); <---this goes wrong.
> $retrievedvalue = $value['retrievedvalue'][0];
>
> Is it implied that the line directly following a foreach statement  
> acts on the line following it?
>
> Is the proper syntax to either leave an empty line, of perhaps  
> empty brackets after the foreach?:
>
> IS THIS BETTER?:
> $findResult = $query->FMFind();
> foreach($findResult['data'] as $key => $value)
> {}
> (SOME OTHER STATEMENTS);
> $retrievedvalue = $value['retrievedvalue'][0];
>
>
> Jonathan
>
> -- 
> Jonathan Schwartz
> Exit 445 Group
> jonathan at exit445.com
> http://www.exit445.com
> 415-370-5011
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list


-|



More information about the FX.php_List mailing list