Explain the syntax for ‘foreach’ loop with example.



Explain the syntax for ‘foreach’ loop with example...

Answer / Ravi Shankar Sharma

The foreach loop in PHP is used to iterate over arrays and objects. Here's an example:
- To iterate over an array:
$fruits = array('apple', 'banana', 'orange');
foreach ($fruits as $fruit) {
echo $fruit;
}
- To iterate over an associative array:
$person = array('name' => 'John', 'age' => 30);
foreach ($person as $key => $value) {
echo $key . ': ' . $value;
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More PHP Interview Questions

Does cors prevent csrf?

1 Answers  


How to reset/destroy a cookie in php?

1 Answers  


Why overriding is called runtime polymorphism?

1 Answers  


What are php magic methods?

1 Answers  


What are the 5 types of data?

1 Answers  


Does winrunner 8.0 or qtp 8.0 supports php

2 Answers  


How can I find the colour of a pixel of an image?

2 Answers   Rushmore Consultancy,


Tell me what is use of in_array() function in php?

1 Answers  


How many different types of messages available in php?

1 Answers  


What is the difference between php4 and php5?

1 Answers  


How do I find out the number of parameters passed into function?

1 Answers  


Is it necessary to use closing tag in php?sometime without closing tag things work.why?

2 Answers   ShopRite,


Categories