Q: What is the function of foreach construct in php?

 Q: What is the function of foreach construct in php?

Ans: The foreach construct in PHP is used to iterate over arrays and objects. It allows you to loop through each element of an array or object and perform a specific action on each iteration. The basic syntax for using the foreach construct is as follows:

foreach ($array as $value) { // code to be executed }

or for object

foreach ($object as $key => $value) { // code to be executed }

Where $array is the array or object you want to loop through, and $value (or $key=>$value) is a variable that will be assigned the current element's value on each iteration of the loop.

No comments:

Post a Comment