Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #15293 > unrolled thread
| Started by | "M. Strobel" <strobel@example.com> |
|---|---|
| First post | 2015-05-06 22:25 +0200 |
| Last post | 2015-05-07 13:36 +0200 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.lang.php
problem with double as array index? "M. Strobel" <strobel@example.com> - 2015-05-06 22:25 +0200
Re: problem with double as array index? "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-06 22:57 +0200
Re: problem with double as array index? adam <adam@nospam.net> - 2015-05-06 21:12 +0000
Re: problem with double as array index? "M. Strobel" <strobel@example.com> - 2015-05-07 00:45 +0200
Re: problem with double as array index? "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-07 01:08 +0200
Re: problem with double as array index? "M. Strobel" <strobel@example.com> - 2015-05-07 13:36 +0200
| From | "M. Strobel" <strobel@example.com> |
|---|---|
| Date | 2015-05-06 22:25 +0200 |
| Subject | problem with double as array index? |
| Message-ID | <midt9v$ihf$1@dont-email.me> |
I stumbled upon this problem when trying to define a trivalent logic:
Interactive test:
php > if (! define('triFalse',0)) trigger_error('Error defining!');
php > if (! define('triMaybe',0.5)) trigger_error('Error defining!');
php > if (! define('triTrue', 1)) trigger_error('Error defining!');
php >
php > $triNotarray = array(triFalse=>triTrue, triMaybe=>triMaybe, triTrue=>triFalse);
php > var_dump($triNotarray);
array(2) {
[0] =>
double(0.5)
[1] =>
int(0)
}
Where is my Maybe value?
I was not aware of this problem (not coding PHP for some time), what is it?
Here the php -v output:
PHP 5.4.20 (cli)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with Xdebug v2.2.6, Copyright (c) 2002-2014, by Derick Rethans
/Str.
[toc] | [next] | [standalone]
| From | "Christoph M. Becker" <cmbecker69@arcor.de> |
|---|---|
| Date | 2015-05-06 22:57 +0200 |
| Message-ID | <midv82$bu3$1@solani.org> |
| In reply to | #15293 |
M. Strobel wrote:
> I stumbled upon this problem when trying to define a trivalent logic:
>
> Interactive test:
>
> php > if (! define('triFalse',0)) trigger_error('Error defining!');
> php > if (! define('triMaybe',0.5)) trigger_error('Error defining!');
> php > if (! define('triTrue', 1)) trigger_error('Error defining!');
> php >
> php > $triNotarray = array(triFalse=>triTrue, triMaybe=>triMaybe,
> triTrue=>triFalse);
> php > var_dump($triNotarray);
> array(2) {
> [0] =>
> double(0.5)
> [1] =>
> int(0)
> }
>
> Where is my Maybe value?
You're triMaybe is there; triFalse is missing, however.
> I was not aware of this problem (not coding PHP for some time), what is it?
Firstly, a simplification:
var_dump(array(0 => 'foo', 0.5 =>'foo', 1 => 'foo'));
Explanation: when a float is used as array key, it is cast to int, see
<http://php.net/manual/en/language.types.array.php#language.types.array.syntax>.
So the second element overwrites the first element.
--
Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | adam <adam@nospam.net> |
|---|---|
| Date | 2015-05-06 21:12 +0000 |
| Message-ID | <Nsv2x.180740$an3.113965@fx23.fr7> |
| In reply to | #15293 |
On 2015-05-06, M. Strobel <strobel@example.com> wrote: > I was not aware of this problem (not coding PHP for some time), what is it? It is not a bug, it is a feature. :) Array key in PHP can either be an integer or a string. So when you use 0.5 it gets auto-casted to an int which gives 0, so: array (0=>1, 0.5=>2, 1=>3) would be equivalent to array (0=>1, 0=>2, 1=>3) No magic here. http://php.net/manual/en/language.types.array.php Regards, A.
[toc] | [prev] | [next] | [standalone]
| From | "M. Strobel" <strobel@example.com> |
|---|---|
| Date | 2015-05-07 00:45 +0200 |
| Message-ID | <mie5fl$l1l$1@dont-email.me> |
| In reply to | #15295 |
On 06.05.2015 23:12, adam wrote: > On 2015-05-06, M. Strobel <strobel@example.com> wrote: >> I was not aware of this problem (not coding PHP for some time), what is it? > It is not a bug, it is a feature. :) > > Array key in PHP can either be an integer or a string. So when you use 0.5 it > gets auto-casted to an int which gives 0, so: > > array (0=>1, 0.5=>2, 1=>3) would be equivalent to array (0=>1, 0=>2, 1=>3) > > No magic here. > > http://php.net/manual/en/language.types.array.php > > Regards, > A. > Okay, got it, index can be an integer or a string, with automatic cast. You have to be an extremely good programmer to circumvent traps like this, so kudos to all PHP programmers writing stable and correct programs. /Str.
[toc] | [prev] | [next] | [standalone]
| From | "Christoph M. Becker" <cmbecker69@arcor.de> |
|---|---|
| Date | 2015-05-07 01:08 +0200 |
| Message-ID | <mie6u1$7na$1@solani.org> |
| In reply to | #15296 |
M. Strobel wrote: > On 06.05.2015 23:12, adam wrote: > >> Array key in PHP can either be an integer or a string. So when you use >> 0.5 it >> gets auto-casted to an int which gives 0, so: >> >> array (0=>1, 0.5=>2, 1=>3) would be equivalent to array (0=>1, 0=>2, >> 1=>3) >> >> No magic here. >> >> http://php.net/manual/en/language.types.array.php > > Okay, got it, index can be an integer or a string, with automatic cast. > > You have to be an extremely good programmer to circumvent traps like > this, so kudos to all PHP programmers writing stable and correct programs. Do I read some irony or even sarcasm here? Anyhow, even for average PHP programmers like me, there are helpful tools such as PHPUnit[1]. A simple unit test could check the expectations. [1] <https://phpunit.de/> -- Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | "M. Strobel" <strobel@example.com> |
|---|---|
| Date | 2015-05-07 13:36 +0200 |
| Message-ID | <mifim4$oen$1@dont-email.me> |
| In reply to | #15297 |
On 07.05.2015 01:08, Christoph M. Becker wrote: > M. Strobel wrote: > >> On 06.05.2015 23:12, adam wrote: >> >>> Array key in PHP can either be an integer or a string. So when you use >>> 0.5 it >>> gets auto-casted to an int which gives 0, so: >>> >>> array (0=>1, 0.5=>2, 1=>3) would be equivalent to array (0=>1, 0=>2, >>> 1=>3) >>> >>> No magic here. >>> >>> http://php.net/manual/en/language.types.array.php >> >> Okay, got it, index can be an integer or a string, with automatic cast. >> >> You have to be an extremely good programmer to circumvent traps like >> this, so kudos to all PHP programmers writing stable and correct programs. > > Do I read some irony or even sarcasm here? I am just trying to cheer up the PHP people. Stupid PHP bashing is below me. > > Anyhow, even for average PHP programmers like me, there are helpful > tools such as PHPUnit[1]. A simple unit test could check the expectations. You should start with defensive coding, by which I mean parameter checking, returncode checking. You can see in my first post that success of define is checked. Then, of course, the tests. /Str.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web