mercredi 31 janvier 2018

PHP clean up code (remove duplicate lines) in if-statement with for-loop

I have an example code, where $number is sometimes set and sometimes not:

//$number = 1;
$maxnumber = 3;
  if (isset($number)){
   echo $number;
} else {
  for ($number = 1; $number<=($maxnumber); $number++) {
   echo $number;
  }
}

As you can see I have a double echo: "echo $number;" now it's only 1 line, but in my real code I have 35 of duplicate lines. So when I change the code I need to change it in two places.

Is there a way (in this example) to get rid of the double "echo $number;", by changing the way I do the for loop or something?

Aucun commentaire:

Enregistrer un commentaire