Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



How to use For-Each Loop in PHP?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 06-03-2010
Bigga Lexx's Avatar
Member
 
Join Date: Aug 2006
Posts: 205
How to use For-Each Loop in PHP?

Hello friends,
I have just started the PHP programming language, so you can consider me as a rookie. I have done with some loop in PHP but I get confused when the for-each loop comes.!! I thought that you guys can explain me in better way. So please tell me how to use For-Each Loop in PHP? Hope that you would help me as soon as possible.!!
__________________
Just a reply to say thank you for these links and posts I have a lot to read and learn now!


Reply With Quote
  #2  
Old 06-03-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
Re: How to use For-Each Loop in PHP?

The loops are the most complex loops in PHP. They work like loops of the C language syntax for loops is as follows :
PHP Code:
for (expr1expr2expr3)
    
statement 
The first expression (expr1) is evaluated (executed), what happens at the beginning of the loop. At the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the statement is executed. If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated (executed).
Reply With Quote
  #3  
Old 06-03-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
Re: How to use For-Each Loop in PHP?

I would like you to tell about the For Loop, since it is an important and complex loop. Expressions can be empty or may contain multiple expressions separated by commas. Expr2 In all expressions separated by a comma are evaluated but the result is obtained since the last part. If expr2 is left blank, meaning it is an infinite loop. PHP implicitly considers it as TRUE, like C. This is not really very useful, unless you want to end the loop by the conditional break.
Reply With Quote
  #4  
Old 06-03-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
Re: How to use For-Each Loop in PHP?

PHP 4 introduced a foreach construct, much like Perl or other languages. It is a simple way to browse a table. Foreach works only on arrays, and return an error if you try to use a variable of another type or uninitialized. The following are two syntaxes of for-each loop :
PHP Code:
foreach (array_expression as $value)
    
statement
foreach (array_expression as $key => $value)
    
statement 
The first form loops over the array array_expression. At each iteration, the value of the current element is assigned to $ value and the internal array pointer is advanced by one. The second form is exactly the same thing, but the key to the current element is assigned to the variable $ key.
Reply With Quote
  #5  
Old 06-03-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
Re: How to use For-Each Loop in PHP?

There are some things that you should know before using the for-each loop into the coding. When foreach first starts, the internal array pointer is automatically reset to the first array element. This means you will not have to call reset () before a foreach loop. Unless the array is a reference, foreach operates on a copy of the specified array and not the array itself. Foreach affects the internal array pointer. Do not use without resetting before.
Reply With Quote
  #6  
Old 06-03-2010
Member
 
Join Date: Nov 2008
Posts: 997
Re: How to use For-Each Loop in PHP?

Since PHP 5, you can easily modify elements of an array by preceding $ value of &. This will assign a reference instead of copying the value. The following example may help you in understanding the topic :
PHP Code:
<?php
$arr 
= array(12345);
foreach (
$arr as &$value) {
    
$value $value 2;
}

unset(
$value);
?>
Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use For-Each Loop in PHP?"
Thread Thread Starter Forum Replies Last Post
Watercooling: Single loop or Dual Loop Akolekar Hardware Peripherals 3 21-10-2011 11:52 PM
Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work mADiRAkSHii Operating Systems 4 25-01-2011 07:23 PM
How to use For Loop in PHP? PsYcHo 1 Software Development 5 28-01-2010 09:35 PM
Which is best for iterator: For loop or while loop Leeland Software Development 4 18-01-2010 06:03 PM
Differentiate between Do-While loop and While loop REDBULL Software Development 3 26-11-2009 10:10 AM


All times are GMT +5.5. The time now is 11:51 AM.