So I have this for loop with 36 if-queries inside. Any advice on making it more efficient?
You can view the complete code here
Here’s a sample of what it looks like:
$numbers = range(1, 36);
shuffle($numbers);
for ($m =0; $m<37; $m++){
if ($numbers[$m] == "1"){
$mirza[$m] = "RUTHIE";
$mage[$m] = "3";
$mquote[$m] = "I get to learn a lot of new things here, like sign language!";
$link[$m] = "http://www.google.com";
}
if ($numbers[$m] == "2"){
$mirza[$m] = "AIDA";
$mage[$m] = "82";
$mquote[$m] = "This is like a club and I know and like all the members. It's good therapy.";
$link[$m] = "/about/";
}
if ($numbers[$m] == "3"){
$mirza[$m] = "AMIRE";
$mage[$m] = "4";
$mquote[$m] = "I am learning how to share and make friends.";
$link[$m] = "/about/";
}
}
When you have this much data it really should be in a database (maybe XML or document style) or a text file.
Still, I’ll show you a way to improve this by hard-coding all the data in one place and eliminating the need for any if or switch statements. You should AT VERY LEAST change to switch statements, better still hard-code all the data in one place (below), better still get a database or CSV text file storing these values.
Just to be clear, the ‘efficiency’ issue here isn’t so much speed (though this way is faster). Its that what you have is a nightmare to maintain and change.
You might do better to seriously reconsider the design of your app, specifically making an object with
mirza, mage, mquote, linkas fields.