TechThat Posted June 28, 2012 Share Posted June 28, 2012 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" Quote Link to comment https://forums.phpfreaks.com/topic/264931-performance-improvment/ Share on other sites More sharing options...
memfiss Posted June 28, 2012 Share Posted June 28, 2012 OMG for ($i=1 ; !($i > 12) ; $i++) { ${'checkmatch'.$i} = isset($_SESSION['match'.$i]) ? $_SESSION['match'.$i] : 0; } Quote Link to comment https://forums.phpfreaks.com/topic/264931-performance-improvment/#findComment-1357673 Share on other sites More sharing options...
TechThat Posted June 28, 2012 Author Share Posted June 28, 2012 Thanks. Learned something new today as well. Guess that's the good thing about being new to something Quote Link to comment https://forums.phpfreaks.com/topic/264931-performance-improvment/#findComment-1357676 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.