Re: array key's: which is correct?
| From: | Paul M Foster | Date: | Tue, 08 Jun 2010 13:28:17 +0000 |
| Subject: | Re: array key's: which is correct? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-305950@lists.php.net to get a copy of this message | ||
On Tue, Jun 08, 2010 at 04:12:42PM +0300, Tanel Tammik wrote:
> Hi,
>
> which one is correct or "better"?
>
> $array[3] = '';
> or
> $array['3'] = '';
If the index for (integer) 3, the first example is correct. If the index
is (string) '3', the second example is correct.
>
> $i = 7;
>
> $array[$i] = '';
> or
> $array["$i"] = '';
>
There's no reason to use "$i". The end result will be the same, but in
the case of "$i", you're forcing the PHP interpreter to interpret the
string "$i", looking for variables (like $i), and output whatever else
is in the string (which in this case is nothing). Also, if $i is an
integer, you have the same problem as above. In the first case, you get
$array[7]. In the second case, you get $array['7'].
Paul
--
Paul M. Foster