what is the diffrence between for and foreach?

Answer Posted / p parimi

For can be used to run statements for a fixed number of
times, foreach can be used to run statements for dynamically
generated arrays(may be result of database query).We can
also used for loop for dynamically generated array(may be
result of database query) but foreach is the ideal way to
iterate dynamic array. The reason behind this, we don't know
the index of array as it may be associate result from
database query. So if we use foreach than it will iterate
the result one by one.

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a controller in programming?

680


List some sorting functions in php?

731


What is a substring in php?

762


What is the use of isset() in php?

760


What is final in php?

735


What is if isset ($_ post submit )) in php?

735


Explain what are the different errors in php?

725


Can php run without apache?

732


How does php work with apache?

720


I am writing an application in php that outputs a printable version of driving directions. It contains some long sentences, and I am a neat freak, and would like to make sure that no line exceeds 50 characters. How do I accomplish that with php?

716


What is session expiry?

718


What is the use of mysql_real_escape_string() function?

701


What is a composer in PHP?

721


how to detect a mobile device using php

792


How does one prevent the following warning ‘warning: cannot modify header information – headers already sent' and why does it occur in the first place?

703