-2

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 =?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Suffice
  • 39
  • 7
  • You have numerous typos and mistakes in your PHP code, making it hard to understand the issue. Exmaples: no quotes around your array, your if() statements are using assignment `=` as opposed to equality tests `==`. Also, it's broken up into several blocks, making it hard to read. Please clean up the sample code first, it will make it easier to answer! Thanks! – OverZealous Aug 05 '11 at 03:12
  • `if($match = 'red')`: All these must be `==`, not `=` – Michael Berkowski Aug 05 '11 at 03:13
  • 1
    Your array values should be in quotes unless they are constants (in which case you should name them better, CAPS or something). You want to look at each element in the array I'm guessing so you should write `$array as $match` **not** `$array[0] as $match`. Your comparisons should be using `==` or `===` not `=` for all three if statements, and you have a random `?>` in the middle of a loop which I highly doubt you want there. – Paul Aug 05 '11 at 03:14
  • 1
    That's just a comment. I have no idea what your question is, so I'm afraid I can't answer. – Paul Aug 05 '11 at 03:15
  • Please read up on PHP before posting. Your PHP code is broken and so is your understanding. – loungerdork Aug 05 '11 at 03:20
  • Heheh. Sorry, I thought it was proof read enough, seems I was mistaken. Should be fixed now. – Suffice Aug 05 '11 at 03:22
  • I have no idea what you mean by *"$other cannot enter the core area without the foreach being in the way"*, however much you **bold** the words. Can you please clarify that? – deceze Aug 05 '11 at 03:22
  • This is #1 of the first reasons echoing out each line of code as it happens is bad practice. – Lawrence Cherone Aug 05 '11 at 03:24
  • Well, it's either the `foreach($array[1] as $match)` or the `$match = $other;` without the brackets from the foreach... If that makes much sense. Trying to get it so that both work. – Suffice Aug 05 '11 at 03:32
  • Suffice it's best you post this question not on SO.. but phpfreaks when I had php problems I posted on that forum and got best answer within seconds, they really love PHP trust me they'll help you out better. – SSpoke Aug 05 '11 at 06:48
  • @SSpoke The problem is not the missing PHP love, it's a completely unclear question. For the sake of trying to provide high-quality, correct answers, we need to understand the problem first. If phpfreaks can correctly answer questions without understanding them, more power to them. – deceze Aug 05 '11 at 07:38

2 Answers2

1
$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.

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
  • 1
    My best guess is he's coming from BASIC, COBOL, or some form of assembly straight into PHP and he's experiencing some cultural shock along the way. This must be one of the ripples. – ZJR Aug 05 '11 at 03:22
  • Hmm, this seems like it just might work.. Would `foreach($array[1] = $other as $match)` do the trick? @ZJR Nah, I'm not just very good with understanding or grammar in general I suspect. – Suffice Aug 05 '11 at 03:35
  • @Suffice No, this line of code adds whatever's in the variable `$other` to the array `$array`. So it'll now contain `red, blue, red, red, red, white`... so a line later when you loop through all the values in `$array`, white will be one of those values. You do not want to change your loop the way you proposed. As others are hinting at, one of those "Learn PHP in 24 hours" type books would probably be a good idea -- you just need a primer on the syntax/constructs, it does feel like you're approaching this from the point of view of a different class of languages. – Dan Grossman Aug 05 '11 at 06:18
0
<?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);

?>

Alternatively:

<?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!'; }
}
?>
Ivan Peevski
  • 963
  • 8
  • 16