How can foreach be used in PHP to iterate through an array of variables and create hyperlinks for each element?
To iterate through an array of variables in PHP and create hyperlinks for each element, you can use the foreach loop to loop through each element and output a hyperlink for each one. Within the loop, you can use the echo statement to output the hyperlinks with the array elements as the link text and link URL.
<?php
// Array of variables
$links = array("Google" => "https://www.google.com", "Facebook" => "https://www.facebook.com", "Twitter" => "https://www.twitter.com");
// Iterate through the array and create hyperlinks
foreach ($links as $text => $url) {
echo "<a href='$url'>$text</a><br>";
}
?>