RE: [PHP] string vs number

2008-02-06 Thread Ford, Mike
On 05 February 2008 21:37, Jochem Maas advised:

 the same is not exactly true for floats - although you can
 use them as array keys you'll
 notice in the output of code below that they are stripped of
 their decimal part (essentially
 a floor() seems to be performed on the float value. I have no
 idea whether this is intentional,
 and whether you can therefore rely on this behaviour:

Yes, and Yes!

From http://php.net/language.types.array:

 A key may be either an integer or a string. If a key is the
 standard representation of an integer, it will be interpreted
 as such (i.e. 8 will be interpreted as 8, while 08 will
 be interpreted as 08). Floats in key are truncated to
 integer.

 --
Mike Ford,  Electronic Information Services Adviser,
JG125, The Headingley Library,
James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730  Fax:  +44 113 812 3211


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm

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



Re: [PHP] string vs number

2008-02-06 Thread Jochem Maas

Ford, Mike schreef:

On 05 February 2008 21:37, Jochem Maas advised:


the same is not exactly true for floats - although you can
use them as array keys you'll
notice in the output of code below that they are stripped of
their decimal part (essentially
a floor() seems to be performed on the float value. I have no
idea whether this is intentional,
and whether you can therefore rely on this behaviour:


Yes, and Yes!

From http://php.net/language.types.array


ah yes, I should have looked it up, that said I find it rather odd that
is works let alone that it's intentional.

though thinking about it you could probably use it for some float val
distribution counting or something. I dunno, seems like it offers a handy
shortcut - although what that shortcut is escapes me just now :-)




A key may be either an integer or a string. If a key is the
standard representation of an integer, it will be interpreted
as such (i.e. 8 will be interpreted as 8, while 08 will
be interpreted as 08). Floats in key are truncated to
integer.


 --
Mike Ford,  Electronic Information Services Adviser,
JG125, The Headingley Library,
James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 812 4730  Fax:  +44 113 812 3211


To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm




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



Re: [PHP] string vs number

2008-02-05 Thread Nathan Nobbe
On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote:

 hi all,

 i have this php statement:

 ? if($rowB[$rowA[0]]=='Y') {echo checked;} ?


 debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = $rowB['54'].

 is this possible?  how do i force $rowA[0] to be a string 
 ('54')?http://www.php.net/unsub.php


php should handle the conversion internally for you.
if you want to type cast a value to a string, simply do

(string)$varname

-nathan


Re: [PHP] string vs number

2008-02-05 Thread Eric Butera
On Feb 5, 2008 1:40 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:
 On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote:

  hi all,
 
  i have this php statement:
 
  ? if($rowB[$rowA[0]]=='Y') {echo checked;} ?
 
 
  debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = $rowB['54'].
 
  is this possible?  how do i force $rowA[0] to be a string 
  ('54')?http://www.php.net/unsub.php


 php should handle the conversion internally for you.
 if you want to type cast a value to a string, simply do

 (string)$varname

 -nathan


I was thinking about saying that, but php is loosely typed, so 54 ==
'54'.  I'm thinking something else is wrong here.

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



Re: [PHP] string vs number

2008-02-05 Thread Nathan Nobbe
On Feb 5, 2008 1:43 PM, Eric Butera [EMAIL PROTECTED] wrote:

 I was thinking about saying that, but php is loosely typed, so 54 ==
 '54'.

i only mentioned the type cast because it was asked about; actually, there
are rare times in php when type casts are called for, such as pulling a
value
from a SimpleXMLElement.  but that is neither here nor there..



 I'm thinking something else is wrong here.

ya, like
$rowB['54'] != 'Y'; // ;)

-nathan


Re: [PHP] string vs number

2008-02-05 Thread Casey
On Feb 5, 2008, at 10:43 AM, Eric Butera [EMAIL PROTECTED]  
wrote:



On Feb 5, 2008 1:40 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:

On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote:


hi all,

i have this php statement:

? if($rowB[$rowA[0]]=='Y') {echo checked;} ?


debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = $rowB 
['54'].


is this possible?  how do i force $rowA[0] to be a string ('54')?http://www.php.net/unsub.php 




php should handle the conversion internally for you.
if you want to type cast a value to a string, simply do

(string)$varname

-nathan



I was thinking about saying that, but php is loosely typed, so 54 ==
'54'.  I'm thinking something else is wrong here.

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



I believe this is the difference with arrays:

$a = array(2 = foo);
Array(0 = null, 1 = null, 2 = foo)

$a = array(2 = foo);
Array(2 = foo)

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



Re: [PHP] string vs number

2008-02-05 Thread Nathan Nobbe
On Feb 5, 2008 1:50 PM, Casey [EMAIL PROTECTED] wrote:

 I believe this is the difference with arrays:

 $a = array(2 = foo);
 Array(0 = null, 1 = null, 2 = foo)

 $a = array(2 = foo);
 Array(2 = foo)



i think the implicit type casting applies there as well:

php  $meh = array(2=4);
php  echo $meh['2'];
4
php  $meh['2'] = 5;
php  echo $meh['2'];
5
php  echo $meh[2];
5

-nathan


Re: [PHP] string vs number

2008-02-05 Thread Eric Butera
On Feb 5, 2008 1:48 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:
 On Feb 5, 2008 1:43 PM, Eric Butera [EMAIL PROTECTED] wrote:

  I was thinking about saying that, but php is loosely typed, so 54 ==
  '54'.
 i only mentioned the type cast because it was asked about; actually, there
  are rare times in php when type casts are called for, such as pulling a
 value
  from a SimpleXMLElement.  but that is neither here nor there..


  I'm thinking something else is wrong here.
 
 ya, like
 $rowB['54'] != 'Y'; // ;)

 -nathan


Yep, I use them all the time.  I just meant that I wasn't sure this is
what was going to get the OP fixed.

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



Re: [PHP] string vs number

2008-02-05 Thread Daniel Brown
On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote:
 hi all,

 i have this php statement:

 ? if($rowB[$rowA[0]]=='Y') {echo checked;} ?


 debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = $rowB['54'].

 is this possible?  how do i force $rowA[0] to be a string ('54')?

Type casting shouldn't be an issue in this case.  For example,
you're not trying to convert alphanumeric characters to int() (which
would actually go to boolean - 0/1), so when trying this case, the
condition is True:

?
$rowA[] = 54;
$rowA[] = 63;
$rowA[] = 72;

$rowB['54'] = Y;
$rowB['63'] = N;
$rowB['72'] = N;

if($rowB[$rowA[0]]=='Y') { echo checked.\n; }
?

Because of the loose-typecasting nature of PHP (done on purpose),
'54' does, in fact, equal 54, unless otherwise specifically stated.

-- 
/Dan

Daniel P. Brown
Senior Unix Geek
? while(1) { $me = $mind--; sleep(86400); } ?

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



Re: [PHP] string vs number

2008-02-05 Thread Jochem Maas

Casey schreef:

On Feb 5, 2008, at 10:43 AM, Eric Butera [EMAIL PROTECTED] wrote:


On Feb 5, 2008 1:40 PM, Nathan Nobbe [EMAIL PROTECTED] wrote:

On Feb 5, 2008 1:36 PM, Hiep Nguyen [EMAIL PROTECTED] wrote:


hi all,

i have this php statement:

? if($rowB[$rowA[0]]=='Y') {echo checked;} ?


debugging, i got $rowA[0] = 54, but i want $rowB[$rowA[0]] = 
$rowB['54'].


is this possible?  how do i force $rowA[0] to be a string 
('54')?http://www.php.net/unsub.php



php should handle the conversion internally for you.
if you want to type cast a value to a string, simply do

(string)$varname

-nathan



I was thinking about saying that, but php is loosely typed, so 54 ==
'54'.  I'm thinking something else is wrong here.

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



I believe this is the difference with arrays:

$a = array(2 = foo);
Array(0 = null, 1 = null, 2 = foo)

$a = array(2 = foo);
Array(2 = foo)


not true:


alice:~ jochem$ php -r '
$a = array(2 = foo); $b = array(2 = foo); var_dump($a, $b);'
array(1) {
  [2]=
  string(3) foo
}
array(1) {
  [2]=
  string(3) foo
}

php treats anything that is the string equivelant of an integer as an integer 
when
it comes to array keys - which comes down to the fact that you cannot therefore 
use
a string version of an integer as an associative key.

so $a[2] and $a[2] are always the same element.

the same is not exactly true for floats - although you can use them as array 
keys you'll
notice in the output of code below that they are stripped of their decimal part 
(essentially
a floor() seems to be performed on the float value. I have no idea whether this 
is intentional,
and whether you can therefore rely on this behaviour:

alice:~ jochem$ php -r '
$a = array(2.5 = foo); $b = array(2.5 = foo); var_dump($a, $b);'
array(1) {
  [2]=
  string(3) foo
}
array(1) {
  [2.5]=
  string(3) foo
}
alice:~ jochem$ php -r '
$a = array(2.6 = foo); $b = array(2.6 = foo); var_dump($a, $b);'
array(1) {
  [2]=
  string(3) foo
}
array(1) {
  [2.6]=
  string(3) foo
}






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