4 Mark PHP
4 Mark PHP
foreach Loop
• Specifically designed for iterating over arrays and objects.
• Does not require an explicit counter.
• Automatically traverses through each element in an array.
• $colors = ["Red", "Green", "Blue"];
• foreach ($colors as $color) {
• echo "$color <br>";
• }
c) Write a PHP Program to display reverse of a string.
Ans. <?php
$str = "Hello";
echo strrev($str);
?>
d) How to create cookies? Give an example.
Ans. <?php
setcookie("user", "John", time() + 3600, "/");
echo "Cookie set successfully!";
?>
$i = 1; $i = 1;
while ($i <= 5) { do {
echo "Iteration $i <br>"; echo "Iteration $i <br>";
$i++; $i++;
} } while ($i <= 5);