Suggestion on how to compact my code

Let’s say that I have the following situation in a piece of code:


if ($continent == 'USA') {
  foreach ($usa_state as $value) {
    // do something with the values $usa_state
  }
else if ($continent == 'Europe') {
  foreach ($europe_state as $value) {
    // do something with the values $europe_state
  }
}

Let’s say that what the code does inside the two foreach loops is the same, it only differs in the values it uses (in the first case it uses the $usa_state array and in the second one it uses $europe_state).

Can this code be somehow shortened?

Is this what you mean?


if ($continent == 'USA') { 
$state = "usa"; 
} 
else if ($continent == 'Europe') { 
$state - "europe"
} 

foreach ($state as $value) { 
 // do something with the values $europe_state 
} 

Or did you mean a variable variable?


$continent = "USA";

if ($continent == 'USA') { 
$state = "usa_state"; 
} 
else if ($continent == 'Europe') { 
$state - "europe_state";
} 
$usa_state= array("AL", "NY");

foreach ( $$state as $value) { 
// do something with the values $europe_state 
echo $value;
} 

// gives AL, NY

What I mean is that I would like to iterate over two different arrays based on the value of the variable $continent, since the code inside the loop is the same, it just iterates over two different arrays.

So yes, something like a variable variable should be good.

Yours is a very good question which gets to the heart of keep it DRY (Don’t Repeat Yourself).

As you chase that dragon down, it will inevitably lead to an awareness of OOP (Object Orientated Programming) (or POO if you are in france, no … really!) then this hunt can start to get really obsessive.

Essentially, one phrase I learned early on in my OOP days was “identify what stays the same” and “separate that which changes” - hence yours is a very insightful question - so if this is one of a few nagging doubts you have about the way you are coding, maybe its time you looked at OOP.

The variable variable is a decent approach – but in other programming languages this is why we have FUNCTIONS. I’d consider combining the two techniques… also depending on how many of them there are, a switch may be better than an if statement.


function handleVar($name) {
	$name.='_state';
	foreach ($$name as $value) {
		// do something with $value
	}
}

switch ($continent) {
	case 'USA':
		handleVar('usa');
	break;
	case 'Europe':
		handleVar('europe');
	break;
}

I’d also consider ditching if/switch alltogether. Name your variables properly, you don’t even need that… this for example is WAY simpler:


$name=strtolower($continent).'_state';
foreach ($$name as $value) {
	// do something with $value
}

Which is WAY simpler… though using that would depend on how much you trust $continent. If it comes from user side you may want to verify it against a list first.

I agree with Cups, it’s time you started learning oop. Specifically abstract classes, the factory design pattern and while your at it, the ArrayIterator interface. Those will solve this problem easily.