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

Is null or empty c#?

516


What is a path Traversal?

559


What is php in simple words?

525


What are arguments in php?

568


How can we set and destroy the cookie in php?

495






How to randomly retrieve a value from an array?

557


What good is polymorphism?

495


Is uploaded file php?

541


What are the environmental variables?

584


What are the advantages of oops in php?

564


What is the function in PHP do not return a timestamp?

572


Difference between array_combine and array_merge?

529


What is $globals php?

538


How can we define a variable accessible in functions of a php script?

553


What is the main function of php?

531