From: "Tanel Tammik" on
Hi,

which one is correct or "better"?

$array[3] = '';
or
$array['3'] = '';

$i = 7;

$array[$i] = '';
or
$array["$i"] = '';


Br
Tanel


From: Paul M Foster on
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
From: Ashley Sheridan on
On Tue, 2010-06-08 at 16:12 +0300, Tanel Tammik wrote:

> Hi,
>
> which one is correct or "better"?
>
> $array[3] = '';
> or
> $array['3'] = '';
>
> $i = 7;
>
> $array[$i] = '';
> or
> $array["$i"] = '';
>
>
> Br
> Tanel
>
>
>


The two indexes are equivalent, although I reckon the integer one will
give better performance over the string.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: Robert Cummings on
Tanel Tammik wrote:
> Hi,
>
> which one is correct or "better"?
>
> $array[3] = '';
> or
> $array['3'] = '';
>
> $i = 7;
>
> $array[$i] = '';
> or
> $array["$i"] = '';

Sometimes it is good to illustrate the correct answer:

<?php

$array = array
(
'1' => '1',
'2' => '2',
'three' => 'three',
'4.0' => '4.0',
5.0 => 5.0,
);

var_dump( array_keys( $array ) );

?>

The answer is surprising (well, not really :) and certainly advocates
against making literal strings of integers or manually converting a
string integer to a real integer or using floating point keys.

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
From: Ashley Sheridan on
On Tue, 2010-06-08 at 09:38 -0400, Robert Cummings wrote:

> Tanel Tammik wrote:
> > Hi,
> >
> > which one is correct or "better"?
> >
> > $array[3] = '';
> > or
> > $array['3'] = '';
> >
> > $i = 7;
> >
> > $array[$i] = '';
> > or
> > $array["$i"] = '';
>
> Sometimes it is good to illustrate the correct answer:
>
> <?php
>
> $array = array
> (
> '1' => '1',
> '2' => '2',
> 'three' => 'three',
> '4.0' => '4.0',
> 5.0 => 5.0,
> );
>
> var_dump( array_keys( $array ) );
>
> ?>
>
> The answer is surprising (well, not really :) and certainly advocates
> against making literal strings of integers or manually converting a
> string integer to a real integer or using floating point keys.
>
> Cheers,
> Rob.
> --
> E-Mail Disclaimer: Information contained in this message and any
> attached documents is considered confidential and legally protected.
> This message is intended solely for the addressee(s). Disclosure,
> copying, and distribution are prohibited unless authorized.
>


Yeah, I found that out the hard way when I was trying to make an array
of Gantt tasks, and realised that all my nice task numbers were changed!

Thanks,
Ash
http://www.ashleysheridan.co.uk


 |  Next  |  Last
Pages: 1 2 3
Prev: Blowfish Encryption
Next: Pagination?