note 34440 modified in control-structures.else by vincent

From: Date: Sat, 20 Sep 2003 12:04:25 +0000
Subject: note 34440 modified in control-structures.else by vincent
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-56807@lists.php.net to get a copy of this message
Both examples below, however, are inefficent. In todays world it means everything to save execution time and be a stickler for this kind of thing. The reason I say this is because of the code which is repeated and does not need to be. Here is my corrected example: <?php if ( !file_exists('./users/' . $HTTP_GET_VARS['user_small'] . '.jpg') ) { $HTTP_GET_VARS['user_small'] = 'notfound'; } ?> <img src="./users/<?php echo $HTTP_GET_VARS['user_small']; ?>" border="0" height="88" width="88"> This new example requires only 238 characters vs. the origional 311 or the "imrpoved" 315. Efficent code will save you much much time in the end. If the above example is multiplied 10 times, 75 bytes are wasted needlessly. 1000 times and you've lost 7.5kb. This may not seem like much, but consider that you have maybe 10 IF statements like this in each file- that 1000 just became 10,000 and it's now 75Kb. That can mean the difference between a page that loads during heavy usage periods and a page that makes the user wait. I recomend you always find the shortet, simpliest way to do your coding as it not only helps reduce clutter but also confusion. I've eliminated an entire second possible branch of code, so you know that if it doesn't return "notfound.jpg", the if statement has -nothing- to do with whatever problem you might have. Clutter is also eliminated as a 900 line file, although still large, is easier to manage than a 1000 line file. --was-- Both examples below, however, are inefficent. In todays world it means everything to save execution time and be a stickler for this kind of thing. The reason I say this is because of the code which is repeated and does not need to be. Here is my corrected example: <?php if ( file_exists('./users/' . $HTTP_GET_VARS['user_small'] . '.jpg') ) { $HTTP_GET_VARS['user_small'] = 'notfound'; } ?> <img src="./users/<?php echo $HTTP_GET_VARS['user_small']; ?>" border="0" height="88" width="88"> This new example requires only 238 characters vs. the origional 311 or the "imrpoved" 315. Efficent code will save you much much time in the end. If the above example is multiplied 10 times, 75 bytes are wasted needlessly. 1000 times and you've lost 7.5kb. This may not seem like much, but consider that you have maybe 10 IF statements like this in each file- that 1000 just became 10,000 and it's now 75Kb. That can mean the difference between a page that loads during heavy usage periods and a page that makes the user wait. I recomend you always find the shortet, simpliest way to do your coding as it not only helps reduce clutter but also confusion. I've eliminated an entire second possible branch of code, so you know that if it doesn't return "notfound.jpg", the if statement has -nothing- to do with whatever problem you might have. Clutter is also eliminated as a 900 line file, although still large, is easier to manage than a 1000 line file. http://www.php.net/manual/en/control-structures.else.php

« previous php.notes (#56807) next »