What is the difference between using -> and => in PHP when iterating through an array?
When iterating through an array in PHP, using the arrow (=>) is used in associative arrays to assign keys to their corresponding values. On the other hand, the dash (->) is used to access properties or methods of an object. If you are iterating through an array, you should use the arrow (=>) to assign keys to values.
// Incorrect usage of dash (->) when iterating through an array
$array = [1, 2, 3];
foreach ($array as $key -> $value) {
echo $key . ': ' . $value . PHP_EOL;
}
// Correct usage of arrow (=>) when iterating through an array
$array = ['a' => 1, 'b' => 2, 'c' => 3];
foreach ($array as $key => $value) {
echo $key . ': ' . $value . PHP_EOL;
}