Jump to content

Performance improvment


TechThat

Recommended Posts

So, I've wrote this code that seems very heavy and unnecessarily long. The code works fine, I was just wondering if there was a better way to write it. It doesn't seems to be very kind to performance(?).

 

The code does a number of thing, I'll try to explain in order:

 

First of, check if $_GET is set:

if ((isset($_GET['matchid']))) {

Then I need to check 12 $_SESSION variables if they are set, and if not giving a control variable the value of 0.

		if((isset($_SESSION['match1']))) {
		$checkmatch1 = $_SESSION['match1'];
	} else {
		$checkmatch1 = 0;
	}
	if((isset($_SESSION['match2']))) {
		$checkmatch2 = $_SESSION['match2'];
	} else {
		$checkmatch2 = 0;
	}
	if((isset($_SESSION['match3']))) {
		$checkmatch3 = $_SESSION['match3'];
	} else {
		$checkmatch3 = 0;
	}
	if((isset($_SESSION['match4']))) {
		$checkmatch4 = $_SESSION['match4'];
	} else {
		$checkmatch4 = 0;
	}
	if((isset($_SESSION['match5']))) {
		$checkmatch5 = $_SESSION['match5'];
	} else {
		$checkmatch5 = 0;
	}
	if((isset($_SESSION['match6']))) {
		$checkmatch6 = $_SESSION['match6'];
	} else {
		$checkmatch6 = 0;
	}
	if((isset($_SESSION['match7']))) {
		$checkmatch7 = $_SESSION['match7'];
	} else {
		$checkmatch7 = 0;
	}
	if((isset($_SESSION['match8']))) {
		$checkmatch8 = $_SESSION['match8'];
	} else {
		$checkmatch8 = 0;
	}
	if((isset($_SESSION['match9']))) {
		$checkmatch9 = $_SESSION['match9'];
	} else {
		$checkmatch9 = 0;
	}
	if((isset($_SESSION['match10']))) {
		$checkmatch10 = $_SESSION['match10'];
	} else {
		$checkmatch10 = 10;
	}
	if((isset($_SESSION['match11']))) {
		$checkmatch11 = $_SESSION['match11'];
	} else {
		$checkmatch11 = 0;
	}
	if((isset($_SESSION['match12']))) {
		$checkmatch12 = $_SESSION['match12'];
	} else {
		$checkmatch12 = 0;
	}

Then I am checking if any of the $_SESSION variables already have the $_GET value:

           switch($_GET['matchid']) {
			case $checkmatch1:
			case $checkmatch2:
			case $checkmatch3:
			case $checkmatch4:
			case $checkmatch5:
			case $checkmatch6:
			case $checkmatch7:
			case $checkmatch8:
			case $checkmatch9:
			case $checkmatch10:
			case $checkmatch11:
			case $checkmatch12:
			$errormsg = 'You cannot select the same match twice!';
			break;

And if not, assigning the $_GET value to the first un-set $_SESSION variable:

                       default:
				if ((!isset($_SESSION['match1']))) {
					$_SESSION['match1'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match2']))) {
					$_SESSION['match2'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match3']))) {
					$_SESSION['match3'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match4']))) {
					$_SESSION['match4'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match5']))) {
					$_SESSION['match5'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match6']))) {
					$_SESSION['match6'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match7']))) {
					$_SESSION['match7'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match8']))) {
					$_SESSION['match8'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match9']))) {
					$_SESSION['match9'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match10']))) {
					$_SESSION['match10'] = $_GET['matchid'];
					$_SESSION['bet10'] = $_GET['bet'];
				} else if ((!isset($_SESSION['match11']))) {
					$_SESSION['match11'] = $_GET['matchid'];
				} else if ((!isset($_SESSION['match12']))) {
					$_SESSION['match12'] = $_GET['matchid'];
				} else {
					$errormsg = 'You cannot select more matches';
				}


	}
}

 

Like I said, the code works fine, I would just like to know if there are some better methods to writing any of the code.

I'm still learning PHP, and would like to "improve my skills" :)

Link to comment
https://forums.phpfreaks.com/topic/264931-performance-improvment/
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.