Re: Question about current() and key()
| From: | Lars Torben Wilson | Date: | Mon, 09 Jun 2003 21:43:41 +0000 |
| Subject: | Re: Question about current() and key() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-150809@lists.php.net to get a copy of this message | ||
On Mon, 2003-06-09 at 14:38, Jim Lucas wrote:
> Does anybody know any benifits to using current() and key()
See below:
> I will show two examples of what I mean.
>
> <?php
>
> echo "<PRE>";
>
> $arr1 = array(array(1 => 10),
> array(2 => 12),
> array(3 => 13),
> array(4 => 14),
> array(5 => 15),
> array(6 => 16),
> array(7 => 17),
> array(8 => 18),
> array(9 => 19)
> );
>
> foreach ( $arr1 AS $value ) {
> echo pow(key($value) , current($value))."\n";
> }
>
> $arr2 = array(1 => 10,
> 2 => 12,
> 3 => 13,
> 4 => 14,
> 5 => 15,
> 6 => 16,
> 7 => 17,
> 8 => 18,
> 9 => 19
> );
>
> foreach ( $arr2 AS $key => $value ) {
> echo pow($key , $value)."\n";
> }
>
> ?>
>
>
> Now for me, these return the same results.
>
> I would think that it would be overhead to use current() and key() in the
> above example.
>
> Can anybody point out why the first one would be better to use and why.
No, you're right. There are situations where you might want to use
these two functions, but this isn't one of them. Doesn't buy you a
thing in this situation. :) There are times I've found them useful,
though, although I won't contrive an example for you...you'll usually
know when you've found one.
> Jim Lucas
--
Torben Wilson <torben@php.net> +1.604.709.0506
http://www.thebuttlesschaps.com http://www.inflatableeye.com
http://www.hybrid17.com http://www.themainonmain.com
-----==== Boycott Starbucks! http://www.haidabuckscafe.com ====-----