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

Which function would you use to replace a record in a database in php?

583


What is putenv?

562


Write a program using while loop?

552


in PHP for pdf which library used?

554


Does csrf token change?

522






Is php used for frontend or backend?

527


How are variables declared in php?

546


How to return ascii value of character in php?

587


What is difference between single quotes and double quotes in php?

486


Tell me what are the different types of errors in php?

506


What is the maximum size of a database in mysql?

523


How can we know the total number of elements of Array?

593


What is the difference between == and === in php?

547


How to define a user function?

568


Explain preg_Match and preg_replace?

545