Jump to content

If Statement Help


n3mesis125

Recommended Posts

Hey Folks,

 

Is there an easier way to do this so I don't need to repeat code, the below statement takes a long time to execute:

 

                                                   if ($arg_list[1] == "func" && $mytime <= $daystamp && $i['func'] == $arg_list[4]){
				if ($timediff <= $calcG){
					$out_g = $out_g + $i[$arg_list[2]];
				} else if ($timediff > $calcG && $timediff <= $mySL){
					$out_y = $out_y + $i[$arg_list[2]];
				} else if ($timediff > $mySL){
					$out_r = $out_r + $i[$arg_list[2]];
				}
			} else if ($arg_list[1] == "lob" && $mytime <= $lobstamp){
				$timediff = $lobstamp - $mytime;
				if ($timediff <= $calcG){
					$out_g = $out_g + $i[$arg_list[2]];
				} else if ($timediff > $calcG && $timediff <= $mySL){
					$out_y = $out_y + $i[$arg_list[2]];
				} else if ($timediff > $mySL){
					$out_r = $out_r + $i[$arg_list[2]];
				}
			}

 

I hate having to repeat the code each time.

 

n3m

Link to comment
https://forums.phpfreaks.com/topic/136271-if-statement-help/
Share on other sites

without seeing the rest of your code (which i can assure you could be cleaner, upon seeing this chunk), here's a suggestion:

 

               if ($timediff <= $calcG){
                  $var_name = 'out_g';
               } else if ($timediff > $calcG && $timediff <= $mySL){
                  $var_name = 'out_y';
               } else if ($timediff > $mySL){
                  $var_name = 'out_r';
               }

 

then you can use the variable's name in a variable variable declaration:

 

// this:
$out_g = $out_g + $i[$arg_list[2]];
// can now be this:
$$var_name += $i[$arglist[2]];

Link to comment
https://forums.phpfreaks.com/topic/136271-if-statement-help/#findComment-710898
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.