Search code examples
phpswitching

PHP - Difficulty switching between code


I'm in a bit of a predicament with a much larger code than this, but this is roughly how it is...

<?php
$other = 'white';

$array = array('red', 'blue', 'red', 'red',  'red');

foreach($array[1] as $match) //OR $match = $other;
{
    //Core Area
    if($match == 'red') { echo 'RED!'; }
    if($match == 'blue') { echo 'BLUE!'; }
    if($match == 'white') { echo 'white!'; }
}
?>

As it is now, $other cannot enter the core area without the foreach being in the way. The alternative being cloning -- via copy n' paste -- to another place. ...Which wont work very well... I've tried placing the area in a function, but without many of global values, it does not seem like a viable option. Is there any way to switch between the foreach and the =?


Solution

  • $array[] = $other;
    

    Now $other is in the array so it'll be in the list of things you compare within your loop.

    Why you want this or what you're really asking is flying over my head.