PHP -难以在代码之间切换


PHP - Difficulty switching between code

我在一个比这大得多的代码有点尴尬,但这大致是如何…

<?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!'; }
}
?>

现在$other 如果没有foreach的阻碍,就不能进入核心区。另一种选择是克隆——通过复制和粘贴——到另一个地方. ...这不会很好地工作……我试过将该区域放在一个函数中,但没有许多全局值,它似乎不是一个可行的选择。是否有办法在foreach=之间切换?

$array[] = $other;

现在$other在数组中,所以它将在循环中比较的列表中。

你为什么想要这个,或者你真正想问的是什么,我都不知道。

<?php
$other = 'white';
$array = array('red', 'blue', 'red', 'red',  'red');
array_push($array, $other);    
foreach($array as $match) //OR $match = $other;
{ 
    //Core Area
  if($match == 'red') { echo 'RED!'; }
  if($match == 'blue') { echo 'BLUE!'; }
  if($match == 'white') { echo 'white!'; }
}
array_pop($array);

?>

另外:

<?php
$other = 'white';
$array = array('red', 'blue', 'red', 'red',  'red');
foreach($array as $match) //OR $match = $other;
{ 
    //Core Area
    custom_match($match);
}
custom_match($other);
function custom_match($color) {
  if($match == 'red') { echo 'RED!'; }
  if($match == 'blue') { echo 'BLUE!'; }
  if($match == 'white') { echo 'white!'; }
}
?>