foreach (iterable_expression as $value) {
    // Code to be executed using $value
}
$fruits = ['apple', 'banana', 'orange'];

foreach ($fruits as $value) {
    echo $value . '<br>';
}
Output
foreach (iterable_expression as $key => $value) {
    // Code to be executed using $key and $value
}
$fruits = [
	'apple' => 'red',
	'banana' => 'yellow',
	'orange' => 'orange'
];

foreach ($fruits as $key => $value) {
    echo 'Fruit: ' . $key . ', Color: ' . $value . '<br>';
}
Output
<?php foreach (iterable_expression as $value): ?>
    <?php // Code block to execute ?>
<?php endforeach; ?>
<?php foreach (iterable_expression as $key => $value): ?>
    <?php // Code block to execute ?>
<?php endforeach; ?>
$fruits = ['apple', 'banana', 'orange'];

$searchValue = 'banana';
$searchIndex = -1;

foreach ($fruits as $index => $value) {
    if ($value === $searchValue) {
        $searchIndex = $index;
        $found = true;
    }
}

if ($searchIndex !== -1) {
    echo "Found '" . $searchValue . "' at index " . $searchIndex . ".";
} else {
    echo "Did not find '" . $searchValue . "'.";
}

// Output: "Found 'banana' at index 1."
$fruits = ['apple', 'banana', 'orange'];

$lastIndex = null;

foreach ($fruits as $index => $value) {
    $lastIndex = $index;
}

if ($lastIndex !== null) {
    echo 'Last index: ' . $lastIndex;
} else {
    echo 'The array is empty.';
}

// Output: "Last index: 2"
$person = [
    'name' => 'John',
    'age' => 30,
    'occupation' => 'Engineer',
    'country' => 'USA'
];

echo 'Personal Information:<br>';

foreach ($person as $key => $value) {
    echo ucfirst($key) . ': ' . $value . '<br>';
}
Output
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

echo 'Even numbers:<br>';

foreach ($numbers as $number) {
    if ($number % 2 === 0) {
        echo $number . '<br>';
    }
}
Output
class Person {
    public $name;
    public $age;
    public $occupation;

    public function __construct($name, $age, $occupation) {
        $this->name = $name;
        $this->age = $age;
        $this->occupation = $occupation;
    }
}

$person = new Person('John', 30, 'Engineer');

foreach ($person as $key => $value) {
    echo ucfirst($key) . ': ' . $value . '<br>';
}
Output
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

echo 'Finding the first even number:<br>';

foreach ($numbers as $number) {
    if ($number % 2 === 0) {
        echo 'First even number: ' . $number . '<br>';
        break;
    }
}
Output
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

echo 'Printing even numbers only:<br>';

foreach ($numbers as $number) {
    if ($number % 2 !== 0) {
        continue;
    }
    echo $number . '<br>';
}
Output
$numbers = [1, 2, 3, 4, 5];
$newNumbers = [];

// Add 1 to each element and store the result in a new array
foreach ($numbers as $number) {
    $newNumbers[] = $number + 1;
}

// Output the contents of the new array
foreach ($newNumbers as $number) {
    echo $number . '<br>';
}
Output
// Original array data
$studentsData = [
    ['name' => 'John', 'age' => 25, 'score' => 85],
    ['name' => 'Alice', 'age' => 23, 'score' => 90]
];

// Initialize an empty associative array (two-dimensional)
$students = [];

// Copy data from $studentsData to $students
foreach ($studentsData as $student) {
    $students[] = $student;
}

// Add a new record
$students[] = ['name' => 'Bob', 'age' => 22, 'score' => 78];

// Output each student's information
foreach ($students as $student) {
    echo 'Name: ' . $student['name'] . ', Age: ' . $student['age'] . ', Score: ' . $student['score'] . '<br>';
}
Output
$numbers = [1, 2, 3, 4, 5];

// Modify array elements by reference
foreach ($numbers as &$value) {
    $value = $value * 2;
}

// Output the modified array
foreach ($numbers as $number) {
    echo $number . '<br>';
}
Output