views:

57

answers:

3

what in your opinion more standard / readable / efficient code of array declaration :

one way :

$days = array(1=>'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

then use : $days[$value]

or the second way :

$days = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

then use : $days[$value-1]

update : i cant sure that the values be in [0-6] , because that i dont offer 3 way.

+1  A: 

Definitely the first one (when keys are correctly defined).

In the second one you need to do a minus (extract) this is an unnecessary cpu cycle makes your code less readable and less maintenable.

Edit: I hope all of you lazy programmers are happy out there.

fabrik
OH My Holy God! A whole CPU cycle! A disaster indeed.
Col. Shrapnel
One cycle, two cycles, three cycles, etc...
fabrik
Yeah and Achilles will never overtake Tortoise
Col. Shrapnel
Sorry, i'm definitely not a fan of dialogues like this.
fabrik
Sure, it require logic.
Col. Shrapnel
Can you tell me what's the logic in poorly written code?
fabrik
+3  A: 

How about the 3rd way:

$days = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

and access it as:

$days[$value]

by ensuring that $value has value between [0,6]

codaddict
This will apply only when we stick with this example.
fabrik
+1 In accordance with my comment on the question.
jensgram
i cant its came from db query that Not up to me
Haim Evgi
+2  A: 

a funny one:

$days = array('Zer','Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

(a friend of mine used a month name "Nulleary" once)
but seriously, it depends on where this array does come from.
For this particular example it should be just date("D",$tstamp);

though the whole problem negligible to me.
I am often using just

$days = explode(" ",'Sun Mon Tue Wed Thu Fri Sat');

and find it very handy.

Col. Shrapnel
yes, but explode costs cpu cycles, thousands of them!
stereofrog
@stereofrog: Thanks, that was funny. It's interesting how lazy programmers lives here.
fabrik
@stereofrog whoops! yu're right. Gonna check my server right now - it must be burning of such overload! :)
Col. Shrapnel
@fabrik: nobody is going to question the importance of optimization, but talking about "cpu cycles" in the context of web scripting is just ridiculous.
stereofrog
@stereofrog: You're totally right. Sorry for the intermezzo.
fabrik