Re: [PHP] Array mysteries

2007-03-12 Thread Richard Lynch

Read the manual again, especially the part about variable scope near
the beginning.


On Sun, March 11, 2007 3:51 am, Otto Wyss wrote:
 I want to convert weekdays with a simple function like

$wdays = array
  (0 = Sonntag
  ,1 = Montag
  ,2 = Dienstag
  ,3 = Mittwoch
  ,4 = Donnerstag
  ,5 = Freitag
  ,6 = Samstag
);

function convert_from_weekday ($weekday) {
  return $wdays[$weekday];
}

 but this doesn't work while

$wdays[$weekday];

 outside of the function works correct. Has anybody an idea where's my
 mistake? I'd like to use a function so I may return substrings of the
 weekday.

 O. Wyss

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-12 Thread Richard Lynch
On Sun, March 11, 2007 12:02 pm, Edward Vermillion wrote:

 On Mar 11, 2007, at 10:02 AM, tedd wrote:

 At 3:05 PM +0100 3/11/07, Tijnema ! wrote:
 On 3/11/07, tedd mailto:[EMAIL PROTECTED][EMAIL PROTECTED]
 wrote:

 At 10:05 AM +0100 3/11/07, Tijnema ! wrote:

 - You could define $wdays inside the function
 function convert_from_weekday ($weekday) {
 $wdays = array
(0 = Sonntag
,1 = Montag
,2 = Dienstag
,3 = Mittwoch
,4 = Donnerstag
,5 = Freitag
,6 = Samstag
  );
return $wdays[$weekday];
  }
 $day = convert_from_weekday(0) // $day = Sonntag

 Tijnema:

 That's also a shorter version of a simple switch statement.

 I haven't thought of, or seen, that before -- thanks.

 tedd


 Yeah it is, but i just used moved his $wdays inside the function...

 but well, there are ofcourse a lot of other options, as date(l)
 would also return the day of the month :)

 Tijnema


 It's the technique and not the specific data thing I was
 addressing. When I'm confronted with a case condition, I typically
 use the switch statement. But, your solution provided me with
 another way to look at that.

 Cheers,

 tedd

 But what's the cost of this in a loop, rebuilding the array each
 time, as compared to a switch statement? Just another thought...

If you are calling this function a ridiculous number of times, you
could use a static variable.

function convert_from_weekday($weekday){
  static $weeks = '';
  if ($weeks === '') $weeks = array('Sonntag', 'Montag', ...);
}

It's kind of a shame that PHP won't let you just initialize a static
to an array, but there it is.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-12 Thread Richard Lynch
On Sun, March 11, 2007 2:57 pm, Edward Vermillion wrote:
 Would the array lookup be faster for a lesser-used option/key in a
 situation where there were quite a few options? (you wouldn't have to
 go through the whole switch to get to the option at the end (?) or
 would you? I have no idea how that all works internally)

Almost for sure, a 'switch' is going to win over the array hash lookup
in almost all cases, as the switch is built into the opcodes, and not
an array hash lookup.

If I understood a thread on Internals last month correctly,
internally, the order of the case statements does matter, somewhat,
but you *DO* have to be calling something a ZILLION times for it to
matter in the real world.

Stressing out over performance instead of maintainability is almost
always the wrong way to start a project.

You don't want to be a complete idiot, but often-times the performance
bottle-neck ends up being somewhere entirely different from where you
expected anyway.

Write the code as straight-forward and maintainable as you can first.

Then measure it for performance to see if it's acceptable.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread Tijnema !

On 3/11/07, Otto Wyss [EMAIL PROTECTED] wrote:


I want to convert weekdays with a simple function like

  $wdays = array
(0 = Sonntag
,1 = Montag
,2 = Dienstag
,3 = Mittwoch
,4 = Donnerstag
,5 = Freitag
,6 = Samstag
  );

  function convert_from_weekday ($weekday) {
return $wdays[$weekday];
  }

but this doesn't work while

  $wdays[$weekday];

outside of the function works correct. Has anybody an idea where's my
mistake? I'd like to use a function so I may return substrings of the
weekday.

O. Wyss



$wdays is not defined inside the function, so you can do a few things:
- Pass the $wdays inside the function:
function convert_from_weekday ($weekday,$wdays) {
   return $wdays[$weekday];
 }
$day = convert_from_weekday(0,$wdays) // $day = Sonntag

- You could define $wdays inside the function
function convert_from_weekday ($weekday,$wdays) {
$wdays = array
   (0 = Sonntag
   ,1 = Montag
   ,2 = Dienstag
   ,3 = Mittwoch
   ,4 = Donnerstag
   ,5 = Freitag
   ,6 = Samstag
 );
   return $wdays[$weekday];
 }
$day = convert_from_weekday(0) // $day = Sonntag
- If you are working from inside a class, you could define $wdays as a
public variable.

I think this solved your problem, but don't hesitate to ask for more!

Tijnema




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Array mysteries

2007-03-11 Thread Steve Edberg

At 9:51 AM +0100 3/11/07, Otto Wyss wrote:

I want to convert weekdays with a simple function like

  $wdays = array
(0 = Sonntag
,1 = Montag
,2 = Dienstag
,3 = Mittwoch
,4 = Donnerstag
,5 = Freitag
,6 = Samstag
  );

  function convert_from_weekday ($weekday) {
return $wdays[$weekday];
  }

but this doesn't work while

  $wdays[$weekday];

outside of the function works correct. Has anybody an idea where's 
my mistake? I'd like to use a function so I may return substrings of 
the weekday.





If the above is your exact code, then the problem is one of scope; 
move the $wdays declaration inside your convert_from_weekday() 
function.


However, you may want to investigate the formatting functions of date():

http://php.he.net/manual/en/function.date.php

Date should return names using the native locale; if you want to 
return date strings for other locales/languages, use setlocale() -


http://php.he.net/manual/en/function.setlocale.php

- and strftime() -

http://php.he.net/manual/en/function.strftime.php

I think you'll find your work has been at least partially done for 
you. And, see


http://php.he.net/manual/en/language.variables.scope.php

for more information on variable scope.

steve

--
+--- my people are the people of the dessert, ---+
| Steve Edberghttp://pgfsun.ucdavis.edu/ |
| UC Davis Genome Center[EMAIL PROTECTED] |
| Bioinformatics programming/database/sysadmin (530)754-9127 |
+ said t e lawrence, picking up his fork +

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread Otto Wyss

Steve Edberg wrote:

At 9:51 AM +0100 3/11/07, Otto Wyss wrote:


  function convert_from_weekday ($weekday) {
return $wdays[$weekday];
  }

If the above is your exact code, then the problem is one of scope; 
move the $wdays declaration inside your convert_from_weekday() 
function.



I'm more used to C++ than PHP, with global $wdays; it works.


However, you may want to investigate the formatting functions of date():

http://php.he.net/manual/en/function.date.php

Date should return names using the native locale; if you want to 
return date strings for other locales/languages, use setlocale() -


http://php.he.net/manual/en/function.setlocale.php



Thanks, I'll keep this in mind.

O. Wyss

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread Satyam


- Original Message - 
From: Otto Wyss [EMAIL PROTECTED]

To: php-general@lists.php.net
Sent: Sunday, March 11, 2007 11:14 AM
Subject: Re: [PHP] Array mysteries



Steve Edberg wrote:

At 9:51 AM +0100 3/11/07, Otto Wyss wrote:


  function convert_from_weekday ($weekday) {
return $wdays[$weekday];
  }

If the above is your exact code, then the problem is one of scope; move 
the $wdays declaration inside your convert_from_weekday() function.



I'm more used to C++ than PHP, with global $wdays; it works.



Unless you are using the array elsewhere, it might be a good idea to put it 
inside the function so as to not polute the global namespace with variable 
names that have no need to be there.  In large applications, there is a 
non-trivial chance that someone else might use a variable by the same name 
and ruin your translation table.  It will be slightly slower, though.



As for the localization functions, you might see their effect in:

http://www.satyam.com.ar/int/setlocale/index.php?locale=de_DEsubmit=Aceptar

the input box allows you to enter different locales, the previous URL 
already has German selected.


Satyam


However, you may want to investigate the formatting functions of date():

http://php.he.net/manual/en/function.date.php

Date should return names using the native locale; if you want to return 
date strings for other locales/languages, use setlocale() -


http://php.he.net/manual/en/function.setlocale.php



Thanks, I'll keep this in mind.

O. Wyss

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 268.18.8/717 - Release Date: 10/03/2007 
14:25





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread tedd

At 10:05 AM +0100 3/11/07, Tijnema ! wrote:


- You could define $wdays inside the function
function convert_from_weekday ($weekday,$wdays) {
$wdays = array
   (0 = Sonntag
   ,1 = Montag
   ,2 = Dienstag
   ,3 = Mittwoch
   ,4 = Donnerstag
   ,5 = Freitag
   ,6 = Samstag
 );
   return $wdays[$weekday];
 }
$day = convert_from_weekday(0) // $day = Sonntag


Tijnema:

That's also a shorter version of a simple switch statement.

I haven't thought of, or seen, that before -- thanks.

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread Tijnema !

On 3/11/07, tedd [EMAIL PROTECTED] wrote:


At 10:05 AM +0100 3/11/07, Tijnema ! wrote:

- You could define $wdays inside the function
function convert_from_weekday ($weekday) {
$wdays = array
(0 = Sonntag
,1 = Montag
,2 = Dienstag
,3 = Mittwoch
,4 = Donnerstag
,5 = Freitag
,6 = Samstag
  );
return $wdays[$weekday];
  }
$day = convert_from_weekday(0) // $day = Sonntag

Tijnema:

That's also a shorter version of a simple switch statement.

I haven't thought of, or seen, that before -- thanks.

tedd



Yeah it is, but i just used moved his $wdays inside the function...

but well, there are ofcourse a lot of other options, as date(l) would also
return the day of the month :)

Tijnema

--

---
http://sperling.com  http://ancientstones.com  http://earthstones.com



Re: [PHP] Array mysteries

2007-03-11 Thread tedd

At 3:05 PM +0100 3/11/07, Tijnema ! wrote:

On 3/11/07, tedd mailto:[EMAIL PROTECTED][EMAIL PROTECTED] wrote:

At 10:05 AM +0100 3/11/07, Tijnema ! wrote:


- You could define $wdays inside the function
function convert_from_weekday ($weekday) {
$wdays = array
   (0 = Sonntag
   ,1 = Montag
   ,2 = Dienstag
   ,3 = Mittwoch
   ,4 = Donnerstag
   ,5 = Freitag
   ,6 = Samstag
 );
   return $wdays[$weekday];
 }
$day = convert_from_weekday(0) // $day = Sonntag


Tijnema:

That's also a shorter version of a simple switch statement.

I haven't thought of, or seen, that before -- thanks.

tedd


Yeah it is, but i just used moved his $wdays inside the function...

but well, there are ofcourse a lot of other options, as date(l) 
would also return the day of the month :)


Tijnema



It's the technique and not the specific data thing I was addressing. 
When I'm confronted with a case condition, I typically use the switch 
statement. But, your solution provided me with another way to look at 
that.


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread Edward Vermillion


On Mar 11, 2007, at 10:02 AM, tedd wrote:


At 3:05 PM +0100 3/11/07, Tijnema ! wrote:

On 3/11/07, tedd mailto:[EMAIL PROTECTED][EMAIL PROTECTED] wrote:

At 10:05 AM +0100 3/11/07, Tijnema ! wrote:


- You could define $wdays inside the function
function convert_from_weekday ($weekday) {
$wdays = array
   (0 = Sonntag
   ,1 = Montag
   ,2 = Dienstag
   ,3 = Mittwoch
   ,4 = Donnerstag
   ,5 = Freitag
   ,6 = Samstag
 );
   return $wdays[$weekday];
 }
$day = convert_from_weekday(0) // $day = Sonntag


Tijnema:

That's also a shorter version of a simple switch statement.

I haven't thought of, or seen, that before -- thanks.

tedd


Yeah it is, but i just used moved his $wdays inside the function...

but well, there are ofcourse a lot of other options, as date(l)  
would also return the day of the month :)


Tijnema



It's the technique and not the specific data thing I was  
addressing. When I'm confronted with a case condition, I typically  
use the switch statement. But, your solution provided me with  
another way to look at that.


Cheers,

tedd


But what's the cost of this in a loop, rebuilding the array each  
time, as compared to a switch statement? Just another thought...


Ed

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread Larry Garfield
On Sunday 11 March 2007 12:02 pm, Edward Vermillion wrote:
  At 10:05 AM +0100 3/11/07, Tijnema ! wrote:
  - You could define $wdays inside the function
  function convert_from_weekday ($weekday) {
  $wdays = array
 (0 = Sonntag
 ,1 = Montag
 ,2 = Dienstag
 ,3 = Mittwoch
 ,4 = Donnerstag
 ,5 = Freitag
 ,6 = Samstag
   );
 return $wdays[$weekday];
   }
  $day = convert_from_weekday(0) // $day = Sonntag

*snip*

  It's the technique and not the specific data thing I was
  addressing. When I'm confronted with a case condition, I typically
  use the switch statement. But, your solution provided me with
  another way to look at that.
 
  Cheers,
 
  tedd

 But what's the cost of this in a loop, rebuilding the array each
 time, as compared to a switch statement? Just another thought...

 Ed

That's why you can just declare it static:

function convert_from_weekday ($weekday) {
static $wdays = array(
0 = Sonntag,
1 = Montag,
2 = Dienstag,
3 = Mittwoch,
4 = Donnerstag,
5 = Freitag,
6 = Samstag
);
return $wdays[$weekday];
}

And then it's only ever defined once, and the lookup is just an array-key 
search that happens down in the engine.  I do this sort of mapping all the 
time.  

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread tedd

At 12:02 PM -0500 3/11/07, Edward Vermillion wrote:

On Mar 11, 2007, at 10:02 AM, tedd wrote:


At 3:05 PM +0100 3/11/07, Tijnema ! wrote:

On 3/11/07, tedd mailto:[EMAIL PROTECTED][EMAIL PROTECTED] wrote:

At 10:05 AM +0100 3/11/07, Tijnema ! wrote:


- You could define $wdays inside the function
function convert_from_weekday ($weekday) {
$wdays = array
   (0 = Sonntag
   ,1 = Montag
   ,2 = Dienstag
   ,3 = Mittwoch
   ,4 = Donnerstag
   ,5 = Freitag
   ,6 = Samstag
 );
   return $wdays[$weekday];
 }
$day = convert_from_weekday(0) // $day = Sonntag


Tijnema:

That's also a shorter version of a simple switch statement.

I haven't thought of, or seen, that before -- thanks.

tedd


Yeah it is, but i just used moved his $wdays inside the function...

but well, there are ofcourse a lot of other options, as date(l) 
would also return the day of the month :)


Tijnema



It's the technique and not the specific data thing I was 
addressing. When I'm confronted with a case condition, I typically 
use the switch statement. But, your solution provided me with 
another way to look at that.


Cheers,

tedd


But what's the cost of this in a loop, rebuilding the array each 
time, as compared to a switch statement? Just another thought...






Ed

It's just another way to look at a possible solution.

As for the cost, what are we talking about? I doubt that a typical 
application would show any discernable difference in execution times.


One could test this easy enough by running both through 50k loops, 
but even then I doubt that the times would be that much different -- 
but I may be wrong, been there before.


Cheers,

tedd
--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread Edward Vermillion


On Mar 11, 2007, at 1:59 PM, tedd wrote:


At 12:02 PM -0500 3/11/07, Edward Vermillion wrote:

On Mar 11, 2007, at 10:02 AM, tedd wrote:


At 3:05 PM +0100 3/11/07, Tijnema ! wrote:
On 3/11/07, tedd mailto:[EMAIL PROTECTED][EMAIL PROTECTED]  
wrote:


At 10:05 AM +0100 3/11/07, Tijnema ! wrote:


- You could define $wdays inside the function
function convert_from_weekday ($weekday) {
$wdays = array
   (0 = Sonntag
   ,1 = Montag
   ,2 = Dienstag
   ,3 = Mittwoch
   ,4 = Donnerstag
   ,5 = Freitag
   ,6 = Samstag
 );
   return $wdays[$weekday];
 }
$day = convert_from_weekday(0) // $day = Sonntag


Tijnema:

That's also a shorter version of a simple switch statement.

I haven't thought of, or seen, that before -- thanks.

tedd


Yeah it is, but i just used moved his $wdays inside the function...

but well, there are ofcourse a lot of other options, as date 
(l) would also return the day of the month :)


Tijnema



It's the technique and not the specific data thing I was  
addressing. When I'm confronted with a case condition, I  
typically use the switch statement. But, your solution provided  
me with another way to look at that.


Cheers,

tedd


But what's the cost of this in a loop, rebuilding the array each  
time, as compared to a switch statement? Just another thought...






Ed

It's just another way to look at a possible solution.

As for the cost, what are we talking about? I doubt that a  
typical application would show any discernable difference in  
execution times.


One could test this easy enough by running both through 50k loops,  
but even then I doubt that the times would be that much different  
-- but I may be wrong, been there before.


Cheers,

tedd


I don't know if there would be any difference either, which is why it  
was a question.


Although Larry's suggestion of making the array static is something I  
hadn't thought of.


Overall it is an interesting concept to use an array instead of a  
switch, and I do wonder at what point, if any, that the two would  
start to diverge resource-wise.


Would the array lookup be faster for a lesser-used option/key in a  
situation where there were quite a few options? (you wouldn't have to  
go through the whole switch to get to the option at the end (?) or  
would you? I have no idea how that all works internally)


Ed

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread Satyam


- Original Message - 
From: Edward Vermillion [EMAIL PROTECTED]

To: tedd [EMAIL PROTECTED]
Cc: Tijnema ! [EMAIL PROTECTED]; php-general@lists.php.net
Sent: Sunday, March 11, 2007 8:57 PM
Subject: Re: [PHP] Array mysteries




On Mar 11, 2007, at 1:59 PM, tedd wrote:


At 12:02 PM -0500 3/11/07, Edward Vermillion wrote:

On Mar 11, 2007, at 10:02 AM, tedd wrote:


At 3:05 PM +0100 3/11/07, Tijnema ! wrote:

On 3/11/07, tedd mailto:[EMAIL PROTECTED][EMAIL PROTECTED]  wrote:

At 10:05 AM +0100 3/11/07, Tijnema ! wrote:


- You could define $wdays inside the function
function convert_from_weekday ($weekday) {
$wdays = array
   (0 = Sonntag
   ,1 = Montag
   ,2 = Dienstag
   ,3 = Mittwoch
   ,4 = Donnerstag
   ,5 = Freitag
   ,6 = Samstag
 );
   return $wdays[$weekday];
 }
$day = convert_from_weekday(0) // $day = Sonntag


Tijnema:

That's also a shorter version of a simple switch statement.

I haven't thought of, or seen, that before -- thanks.

tedd


Yeah it is, but i just used moved his $wdays inside the function...

but well, there are ofcourse a lot of other options, as date (l) 
would also return the day of the month :)


Tijnema



It's the technique and not the specific data thing I was  addressing. 
When I'm confronted with a case condition, I  typically use the switch 
statement. But, your solution provided  me with another way to look at 
that.


Cheers,

tedd


But what's the cost of this in a loop, rebuilding the array each  time, 
as compared to a switch statement? Just another thought...






Ed

It's just another way to look at a possible solution.

As for the cost, what are we talking about? I doubt that a  typical 
application would show any discernable difference in  execution times.


One could test this easy enough by running both through 50k loops,  but 
even then I doubt that the times would be that much different  -- but I 
may be wrong, been there before.


Cheers,

tedd


I don't know if there would be any difference either, which is why it  was 
a question.


Although Larry's suggestion of making the array static is something I 
hadn't thought of.


Overall it is an interesting concept to use an array instead of a  switch, 
and I do wonder at what point, if any, that the two would  start to 
diverge resource-wise.


Would the array lookup be faster for a lesser-used option/key in a 
situation where there were quite a few options? (you wouldn't have to  go 
through the whole switch to get to the option at the end (?) or  would 
you? I have no idea how that all works internally)


Yes, you would.  It goes sequentially through each case:.  The array, on the 
other hand, uses a hashing algorithm so it should be about even no matter 
which option you pick.


Satyam

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Array mysteries

2007-03-11 Thread Tijnema !

On 3/11/07, Satyam [EMAIL PROTECTED] wrote:



- Original Message -
From: Edward Vermillion [EMAIL PROTECTED]
To: tedd [EMAIL PROTECTED]
Cc: Tijnema ! [EMAIL PROTECTED]; php-general@lists.php.net
Sent: Sunday, March 11, 2007 8:57 PM
Subject: Re: [PHP] Array mysteries



 On Mar 11, 2007, at 1:59 PM, tedd wrote:

 At 12:02 PM -0500 3/11/07, Edward Vermillion wrote:
 On Mar 11, 2007, at 10:02 AM, tedd wrote:

 At 3:05 PM +0100 3/11/07, Tijnema ! wrote:
 On 3/11/07, tedd mailto:[EMAIL PROTECTED][EMAIL PROTECTED]
  wrote:

 At 10:05 AM +0100 3/11/07, Tijnema ! wrote:

 - You could define $wdays inside the function
 function convert_from_weekday ($weekday) {
 $wdays = array
(0 = Sonntag
,1 = Montag
,2 = Dienstag
,3 = Mittwoch
,4 = Donnerstag
,5 = Freitag
,6 = Samstag
  );
return $wdays[$weekday];
  }
 $day = convert_from_weekday(0) // $day = Sonntag

 Tijnema:

 That's also a shorter version of a simple switch statement.

 I haven't thought of, or seen, that before -- thanks.

 tedd


 Yeah it is, but i just used moved his $wdays inside the function...

 but well, there are ofcourse a lot of other options, as date (l)
 would also return the day of the month :)

 Tijnema


 It's the technique and not the specific data thing I was  addressing.
 When I'm confronted with a case condition, I  typically use the
switch
 statement. But, your solution provided  me with another way to look
at
 that.

 Cheers,

 tedd

 But what's the cost of this in a loop, rebuilding the array
each  time,
 as compared to a switch statement? Just another thought...




 Ed

 It's just another way to look at a possible solution.

 As for the cost, what are we talking about? I doubt that a  typical
 application would show any discernable difference in  execution times.

 One could test this easy enough by running both through 50k loops,  but
 even then I doubt that the times would be that much different  -- but I
 may be wrong, been there before.

 Cheers,

 tedd

 I don't know if there would be any difference either, which is why
it  was
 a question.

 Although Larry's suggestion of making the array static is something I
 hadn't thought of.

 Overall it is an interesting concept to use an array instead of
a  switch,
 and I do wonder at what point, if any, that the two would  start to
 diverge resource-wise.

 Would the array lookup be faster for a lesser-used option/key in a
 situation where there were quite a few options? (you wouldn't have
to  go
 through the whole switch to get to the option at the end (?) or  would
 you? I have no idea how that all works internally)

Yes, you would.  It goes sequentially through each case:.  The array, on
the
other hand, uses a hashing algorithm so it should be about even no matter
which option you pick.

Satyam



PHP is always fast, as long as you are not trying to do this 50k times, does
it make sense if a function takes 0.0056 or 0.0057 seconds to
execute?
I don't think so, so that means this is all going about users preference.

Tijnema


Re: [PHP] Array mysteries

2007-03-11 Thread Robert Cummings
On Sun, 2007-03-11 at 21:41 +0100, Satyam wrote:
 - Original Message - 
 From: Edward Vermillion [EMAIL PROTECTED]
 To: tedd [EMAIL PROTECTED]
 Cc: Tijnema ! [EMAIL PROTECTED]; php-general@lists.php.net
 Sent: Sunday, March 11, 2007 8:57 PM
 Subject: Re: [PHP] Array mysteries
 
 
 
  On Mar 11, 2007, at 1:59 PM, tedd wrote:
 
  At 12:02 PM -0500 3/11/07, Edward Vermillion wrote:
  On Mar 11, 2007, at 10:02 AM, tedd wrote:
 
  At 3:05 PM +0100 3/11/07, Tijnema ! wrote:
  On 3/11/07, tedd mailto:[EMAIL PROTECTED][EMAIL PROTECTED]  wrote:
 
  At 10:05 AM +0100 3/11/07, Tijnema ! wrote:
 
  - You could define $wdays inside the function
  function convert_from_weekday ($weekday) {
  $wdays = array
 (0 = Sonntag
 ,1 = Montag
 ,2 = Dienstag
 ,3 = Mittwoch
 ,4 = Donnerstag
 ,5 = Freitag
 ,6 = Samstag
   );
 return $wdays[$weekday];
   }
  $day = convert_from_weekday(0) // $day = Sonntag
 
  Tijnema:
 
  That's also a shorter version of a simple switch statement.
 
  I haven't thought of, or seen, that before -- thanks.
 
  tedd
 
 
  Yeah it is, but i just used moved his $wdays inside the function...
 
  but well, there are ofcourse a lot of other options, as date (l) 
  would also return the day of the month :)
 
  Tijnema
 
 
  It's the technique and not the specific data thing I was  addressing. 
  When I'm confronted with a case condition, I  typically use the switch 
  statement. But, your solution provided  me with another way to look at 
  that.
 
  Cheers,
 
  tedd
 
  But what's the cost of this in a loop, rebuilding the array each  time, 
  as compared to a switch statement? Just another thought...
 
 
 
 
  Ed
 
  It's just another way to look at a possible solution.
 
  As for the cost, what are we talking about? I doubt that a  typical 
  application would show any discernable difference in  execution times.
 
  One could test this easy enough by running both through 50k loops,  but 
  even then I doubt that the times would be that much different  -- but I 
  may be wrong, been there before.
 
  Cheers,
 
  tedd
 
  I don't know if there would be any difference either, which is why it  was 
  a question.
 
  Although Larry's suggestion of making the array static is something I 
  hadn't thought of.
 
  Overall it is an interesting concept to use an array instead of a  switch, 
  and I do wonder at what point, if any, that the two would  start to 
  diverge resource-wise.
 
  Would the array lookup be faster for a lesser-used option/key in a 
  situation where there were quite a few options? (you wouldn't have to  go 
  through the whole switch to get to the option at the end (?) or  would 
  you? I have no idea how that all works internally)
 
 Yes, you would.  It goes sequentially through each case:.  The array, on the 
 other hand, uses a hashing algorithm so it should be about even no matter 
 which option you pick.

Not quite. When the number of possible keys are small, a rote traversal
using language constructs such as if/elseif/else/case is likely to be
faster than incurring the overhead of the hash search. However, in
general the hash search will be faster. Having said that, the use of an
array to hold the key/value pairs produces a very succinct and readable
solution. Additionally, retrieval of such values from the database is
more easily implemented using the array methodology. For those not aware
of using static hash lookups with database results I've included an
example:

?php

function convert_from_weekday( $weekday )
{
static $days = null;

if( $days === null )
{
$query =
 SELECT 
weekday_id 
weekday_name 
FROM 
weekday_table ;

$days = array();
if( $db-query( $query ) )
{
while( $db-nextRow() )
{
$days[$db-getField( 'weekday_id' )] =
$days[$db-getField( 'weekday_name' )];
}
}
}

if( isset( $days[$weekday] ) )
{
return $days[$weekday];
}

return null;
}

?

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php