FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » integer and string what's the difference ?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
integer and string what's the difference ? [message #175860] Tue, 01 November 2011 13:19 Go to next message
gaoqiang is currently offline  gaoqiang
Messages: 1
Registered: November 2011
Karma: 0
Junior Member
1 <?php
2 require_once("config.php");
3 $now=time();
4 $value="abcd";
5 $key="1234";
6
7 $mem_server="localhost";
8 $mem_port=11211;
9 $memc=new Memcached;
10 $memc->addServer($mem_server,$mem_port);
11 $memc->set($key,$value);
12 $keys=array();
13 array_push($keys, 1234);
14 $rsts=$memc->getMulti($keys);
15 var_dump($rsts);
16 ?>

line 13: when using array_push($keys,"1234"),
it works fine. but not with 1234.

when using $memc->get,not getMulti, 1234 and
"1234" are the same.

I think this is a bug. any idea ?
Re: integer and string what's the difference ? [message #175861 is a reply to message #175860] Tue, 01 November 2011 15:23 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
On Tue, 1 Nov 2011 06:19:01 -0700 (PDT), gaoqiang wrote:

> 1 <?php
> 2 require_once("config.php");
> 3 $now=time();
> 4 $value="abcd";
> 5 $key="1234";
> 6
> 7 $mem_server="localhost";
> 8 $mem_port=11211;
> 9 $memc=new Memcached;
> 10 $memc->addServer($mem_server,$mem_port);
> 11 $memc->set($key,$value);
> 12 $keys=array();
> 13 array_push($keys, 1234);
> 14 $rsts=$memc->getMulti($keys);
> 15 var_dump($rsts);
> 16 ?>
>
> line 13: when using array_push($keys,"1234"),
> it works fine. but not with 1234.
>
> when using $memc->get,not getMulti, 1234 and
> "1234" are the same.
>
> I think this is a bug. any idea ?

An integer is any numeric value.
Where some will argue that an integer is any whole number like 0 or 1.
But is "1" the same as 1? No it is not.

I once came across a web page that asked this simple question:
When does '1'+'1'='11'?
Notice the quotes.
in computer math 1+1=2.
But '1'+'1'='11'


In your code above, check line 13.
In line 5 you defined $key="1234".
In line 13, you are treating this value as an integer, not a string as you
defined it.
The quotes define the value as a string.
Try using the quotes around 1234 and see what happens.

In BASIC, a statement like a$="hello"+world would generate an error.
So to be correct it would have to be like this:
a$="hello"+str$(world)
str$() converts an integer to a string.
Re: integer and string what's the difference ? [message #175862 is a reply to message #175861] Tue, 01 November 2011 17:25 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
On 11/1/2011 11:23 AM, richard wrote:

> An integer is any numeric value.

Absolute nonsense. 123.456 is a numeric value, but it is not an integer.

> Where some will argue that an integer is any whole number like 0 or 1.

That is, in fact, the definition of an integer: a number that has no
fractional part. There is no "argument" about it, except by the uninformed.

C, the set of counting numbers, is {1, 2, 3, ...}
W, the set of whole numbers, is C and zero: {0, 1, 2, 3, ...}
I, the set of integers, is W and the negatives of C: { ..., -3, -2, -1,
0, 1, 2, 3, ...}
Re: integer and string what's the difference ? [message #175863 is a reply to message #175861] Tue, 01 November 2011 18:48 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 01 Nov 2011 11:23:39 -0400, richard wrote:

> On Tue, 1 Nov 2011 06:19:01 -0700 (PDT), gaoqiang wrote:
>
>> 1 <?php
>> 2 require_once("config.php");
>> 3 $now=time();
>> 4 $value="abcd";
>> 5 $key="1234";
>> 6
>> 7 $mem_server="localhost";
>> 8 $mem_port=11211;
>> 9 $memc=new Memcached;
>> 10 $memc->addServer($mem_server,$mem_port); 11
>> $memc->set($key,$value);
>> 12 $keys=array();
>> 13 array_push($keys, 1234);
>> 14 $rsts=$memc->getMulti($keys);
>> 15 var_dump($rsts);
>> 16 ?>

> An integer is any numeric value.

Crap. An integer is any whole number. fractional values eg 1/2, 1/4,
567/24 etc are not integers, but they are numbers.

> Where some will argue that an integer is any whole number like 0 or 1.
> But is "1" the same as 1? No it is not.
>
> I once came across a web page that asked this simple question: When does
> '1'+'1'='11'?
> Notice the quotes.
> in computer math 1+1=2.
> But '1'+'1'='11'

Totally irrelevant. The OP clearly knows the difference between strings
and integers.

> In your code above, check line 13.
> In line 5 you defined $key="1234".
> In line 13, you are treating this value as an integer, not a string as
> you defined it.

>> 5 $key="1234";
>> 13 array_push($keys, 1234);

What on earth are you talking about? line 5 defines $key, line 13 uses
$keys, 2 totally different variables.

Richard, stop trying to debug people's code, you really have no idea what
you're talking about most of the time .... which I guess is nothing new.

Rgds

Denis McMahon
Re: integer and string what's the difference ? [message #175864 is a reply to message #175860] Tue, 01 November 2011 19:07 Go to previous messageGo to next message
Balazs Nadasdi is currently offline  Balazs Nadasdi
Messages: 7
Registered: November 2011
Karma: 0
Junior Member
http://www.php.net/manual/en/memcache.set.php
http://www.php.net/manual/en/memcache.get.php

bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )
string Memcache::get ( string $key [, int &$flags ] )
array Memcache::get ( array $keys [, array &$flags ] )


If you add an integer based key, PHP convert it into string (get or set).
I think if you are add as array (key list) PHP doesn't convert it into string (each item). But when you try to get a single value (1234) as an integer PHP automatically convert it because the function needs a string.

Difference between Integer and String... In php:
"1234" == 1234 # => true
"1234" === 1234 # => false


I hope, I was able to help you with something (and I don't misunderstand your problem).
Re: integer and string what's the difference ? [message #175865 is a reply to message #175860] Tue, 01 November 2011 19:23 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 01 Nov 2011 06:19:01 -0700, gaoqiang wrote:

> line 13: when using array_push($keys,"1234"), it works fine. but not
> with 1234.

Memcached::set() and Memcached::get() both look for a string as the first
(ie key) parameter.

public bool Memcached::set ( string $key , mixed $value [, int
$expiration ] )

public mixed Memcached::get ( string $key [, callback $cache_cb [, float &
$cas_token ]] )

This suggests to me that keys must be strings, and so an array of keys
must be an array of string values. Hence the numeric value 1234 may not
be a valid key value even if it's in an array.

Rgds

Denis McMahon
Re: integer and string what's the difference ? [message #175866 is a reply to message #175865] Tue, 01 November 2011 23:03 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 01 Nov 2011 19:23:25 +0000, Denis McMahon wrote:

> This suggests to me that keys must be strings, and so an array of keys
> must be an array of string values. Hence the numeric value 1234 may not
> be a valid key value even if it's in an array.

Following up on my own post, more testing suggests get / set appear to
convert numeric keys to strings, but getMulti and setMulti don't seem to
do so. I'd suggest an issue with php memcached, although the misbehaviour
could conceivably be in the underlying server or client and not the php
client wrapper.

As you're using the php wrapper, that's probably where you should raise
the issue.

Rgds

Denis McMahon
Re: integer and string what's the difference ? [message #175867 is a reply to message #175866] Tue, 01 November 2011 23:36 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 01 Nov 2011 23:03:02 +0000, Denis McMahon wrote:

> As you're using the php wrapper, that's probably where you should raise
> the issue.

Or vote on https://bugs.php.net/bug.php?id=59016

Rgds

Denis McMahon
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: missing variable
Next Topic: Stats comp.lang.php (last 7 days)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Dec 01 12:00:47 GMT 2024

Total time taken to generate the page: 0.02283 seconds