Forum Moderators: coopster
For EG
if currentcond = "sunny"
if ( $currentcond == "Possible shower" ¦¦ "Showers easing" ¦¦ "Showers increasing")
{
$conditions = "<img src=\"images/possible_shower.gif\" width=\"53\" height=\"39\"><br>$currentcond";
}
it would still show the showers icon..
i know this is wrong but i cant find anywhere that shows how to do it correctly
[pre]
switch( $currentcond ) {
case 'sunny':
print '<img src="images/sun.gif"><br />'.$currentcond;
break;
case 'Possible shower':
case 'Showers easing':
case 'Showers increasing':
print '<img src="images/showers.gif"><br />'.$currentcond;
break;
default:
print '<img src="images/question.gif"><br />Who Knows?';
}
[/pre]
[pre]
$conditionToPic = array(
'sunny' => 'sunny.gif',
'Possible shower' => 'shower.gif',
'Showers easing' => 'shower.gif',
'Showers increasing' => 'heavyrain.gif',
'Snow' => 'snowflake.gif' );
if ( isset($conditionToPic[$currentCond]) )
print '<img src="'.$conditionToPic[$currentcond].'"><br />'.$currentcond;
[/pre]
That way you can move the actual mapping to a config.php file so you can find it easier at a later date.
PHP's IF statement will quite as soon as it's sure of an answer.
ie with:
if ( $currentcond == "Possible shower" ¦¦ "Showers easing" ¦¦ "Showers increasing")
if $currentcond == "Possible shower" is true then the statement does not evaluate the rest of the statement and jumps right to the code block because if knows that all the options are "OR'd" so that any TRUE is an overall TRUE.
grahamstewart I like your array solutions for this over the case it's a more natural fit but much more overhead compared to the 1 liner if statement.
daisho
jatar_k's solution would be the fastest for parsing and executing.
Doubt it.
If anything the Switch statement should actually be (very marginally) faster than if, because you are explicity stating that you are doing all your compares against the same variable.
And the array solution will be faster still for the average case, since arrays use hashing.
PHP's IF statement will quit as soon as it's sure of an answer.
So will switch. And the array.
array solutions... much more overhead
Much more overhead? Not really.
Okay so I create a global array, but its not exactly a huge drain on resources. Couple of bytes will make no difference.
However, the point I'm trying to make in all this is:
Don't get too hung up on 'efficiency'. Especially at this level.
Often you will be making false assumptions anyway, and even if you do manage to save a couple of bytes of stack space or a few CPU clock cycles - the difference will be so infinitesimally tiny that you'd have to run the function a couple of million times to see it.
Instead write your code so that it is easy to understand and flexible. Something like 70% of the development cycle is spent in maintentance [htc.honeywell.com] so the easier it is to maintain the simpler your life will be.
Though I do always try to balance speed and readability, I agree that in this case the difference would take ages to notice, but at a point it still does make a difference. If you always say "What does it matter a few milliseconds here a few milliseconds their" they end up adding up. If you had a large system with 100 places that look a bit of extra time then had them in a loop all of a sudden you code is running 10s slower to generate a webpage.
Looking at each piece you'd never guess. That's why I always try to err on the side of speed.
daisho.
For the other 5% that is within loops that get called often or have a very large number of iterations then speed can take more of a priority.
Out of interest I wrote this little script. It executes each of the three techniques mentioned one million times. For fairness sake, I made the currentcond 'Showers easing' which is the third option in each technique.
[pre][1]
<?php
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$iter = 1000000;
$currentcond = 'Showers easing';
$starttime= getmicrotime();
for ( $x = 0; $x < $iter; $x++ ) {
if ( $currentcond == 'sunny' )
$result = '<img src="images/sun.gif"><br />'.$currentcond;
else if ( $currentcond == "Possible shower" ¦¦ $currentcond == "Showers easing" ¦¦ $currentcond == "Showers increasing")
{
$result = '<img src="images/sun.gif"><br />'.$currentcond;
}
}
$endtime = getmicrotime();
$time = $endtime - $starttime;
print 'If: '.$time." seconds\n";
$starttime = getmicrotime();
for ( $x = 0; $x < $iter; $x++ ) {
switch ( $currentcond ) {
case 'sunny':
$result = '<img src="images/sun.gif"><br />'.$currentcond;
break;
case 'Possible shower':
case 'Showers easing':
case 'Showers increasing':
$result = '<img src="images/showers.gif"><br />'.$currentcond;
break;
default:
$result = '<img src="images/question.gif"><br />Who Knows?';
}
}
$endtime = getmicrotime();
$time = $endtime - $starttime;
print 'Switch: '.$time." seconds\n";
$starttime= getmicrotime();
$conditionToPic = array(
'sunny' => 'sunny.gif',
'Possible shower' => 'shower.gif',
'Showers easing' => 'shower.gif',
'Showers increasing' => 'heavyrain.gif',
'Snow' => 'snowflake.gif' );
for ( $x = 0; $x < $iter; $x++ ) {
if ( isset($conditionToPic[$currentcond]) )
$result = '<img src="'.$conditionToPic[$currentcond].'"><br />'.$currentcond;
}
$endtime = getmicrotime();
$time = $endtime - $starttime;
print 'Array: '.$time." seconds\n";
?>
[/1][/pre]
The results on my box are:
If: 6.4893039465 seconds
Switch: 5.79472005367 seconds
Array: 5.01467204094 seconds
So in other words, for this case, the array method is faster by just over one millionth of a second.
Since this code is only run once per page it doesn't really matter which technique you use. So go with the one that you find readable and which suits your purpose best.
The point I was trying to make is that your talking about a difference of around one millionth of a second - so for a once in a page operation like this it really doesn't matter a jot which you use.
In fact, unless it's something that gets executed thousands of times, it's just not worth worrying about.
Don't be swayed by false efficiency - 'faster' code is not always 'better' code. Use the code that suits your situation and is the most readily understandable and readable.