Jump to content

Avoid Variable Declaration.


190319m9

Recommended Posts

<?php        echo date("H") . "<br />";        $H=date("H");        if ($H>5 && $H<12)            echo "Good Morning";        elseif ($H=12)            echo "Good Noon";        elseif ($H>12 && $H<4)            echo "Good Afternoon";        elseif ($H>4 && $H<8)            echo "Good Evening";        else            echo "Good Night";    ?>

Is it must to declare time as a variable? Can't we use the date tie function directly to compare with the numerical values? Tried that but code doesn't work in that case.

Link to comment
Share on other sites

No. It's not a must. But like you were told when you were given that code - it's faster than using the date() function directly multiple times.Again, you can, but it will be slower.Here's a legal, but much slower version of your code:

<?php		echo date("H") . "<br />";		if (date("H")>5 && date("H")<12)			echo "Good Morning";		elseif (date("H")=12)			echo "Good Noon";		elseif (date("H")>12 && date("H")<4)			echo "Good Afternoon";		elseif (date("H")>4 && date("H")<8)			echo "Good Evening";		else			echo "Good Night";	?>

Also, you avoid a certain edge case - Imagine the PHP file starts at, for example 3:59:59:99 (H:MM:SS:ms format), and reaches 4:00:00:00 when it's time for that check. If you store the initial time, you'll get a greeting for that time. Otherwise, you'll get a greeting for the new time. Even worse is if you do more than a greeting. You may start doing things for the afternoon, and end up doing things for the evening.

Link to comment
Share on other sites

<?php        echo date("H") . "<br />";        $H=date("H");        if ($H>5 && $H<12)            echo "Good Morning";        elseif ($H=12)            echo "Good Noon";        elseif ($H>12 && $H<4)            echo "Good Afternoon";        elseif ($H>4 && $H<8)            echo "Good Evening";        else            echo "Good Night";    ?>

Is it must to declare time as a variable? Can't we use the date tie function directly to compare with the numerical values? Tried that but code doesn't work in that case.

You have a few problems here:
  • The computer can't tell if you're referring to AM or PM.The system you're using is a 24h one: From 12pm and on it passes to 13, then 14 and then 15, on until 23. Then it changes to 0.
  • $H>12 && $H<4 No number is greater than 12 and less than 4 at the same time, use logic.
  • $H=12 This statement is assigning 12 to the $H variable, not comparing it. To compare, use the comparative ==

Now, to answer your question, it's better to assign the hour to a variable, because in the unlikely case that the hour changes between the time that you're performing comparisons your program will work incorrectly. Other than that, the computer only will have to check the date once, so your application would run faster.

Link to comment
Share on other sites

Moreover, unless you expect the result to change (as with token calls, file reads, and some database calls) the principle we've been discussing here applies generally. If you expect to use a function result more than once, store it in a variable and then reference the variable. Not just the Date() function. All functions, especially the ones you write yourself (since an interpreted function runs much more slowly than a compiled function).

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...