How can PHP variables be accessed across different loops in a script?

To access PHP variables across different loops in a script, you can declare the variable outside of the loops so that it is in the global scope. This way, the variable will be accessible from any part of the script, including inside loops.

$myVariable = "Hello";

for ($i = 0; $i < 3; $i++) {
    echo $myVariable . " ";
}

foreach ([1, 2, 3] as $num) {
    echo $myVariable . " ";
}