Explain the syntax of the "foreach" loop with an example.
TechnicalSenior php developerPHP DeveloperMid PHP DeveloperThe foreach loop in PHP is used to iterate over arrays. The syntax is: foreach ($array as $value) { // code }. For example, if I have an array $fruits with values like 'apple', 'banana', and 'cherry', I can loop through it as follows:
foreach ($fruits as $fruit) { echo $fruit; }
This will output each fruit. The foreach loop is efficient and simplifies array handling without needing an index.