Re: [PHP] strcmp()?

2011-05-24 Thread Vitalii Demianets
On Monday 23 May 2011 15:00:27 tedd wrote:
 Are you absolutely certain about that?
 
     echo strcmp('These are nearly equal', 'These are almost equal'), \n;
     echo strcmp('different', 'unequal'), \n;
     echo strcmp('b', 'a'), br /\n;

My result is:
13 -17 1

I'm runninng PHP 5.2.14 on the ARM Linux with uClibc.
Certainly the result depends on architecture and/or libc, because PHP just 
calls system strcmp, nothing more.
So. to write compatible scripts one should check  0, not == -1.

-- 
With Best Regards,
Vitalii Demianets
Head engineer
Factor-SPE
Kiev, Ukraine
tel/fax: +380(44)249-21-63

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



Re: [PHP] strcmp()?

2011-05-24 Thread David Harkness
On Tue, May 24, 2011 at 12:48 AM, Vitalii Demianets vi...@nppfactor.kiev.ua
 wrote:

 So. to write compatible scripts one should check  0, not == -1.


Which matches the documentation:

Returns  0 if str1 is less than str2;  0 if str1 is greater than str2,
and 0 if they are equal.

David


Re: [PHP] strcmp()?

2011-05-24 Thread tedd

At 11:10 AM -0700 5/24/11, David Harkness wrote:

On Tue, May 24, 2011 at 12:48 AM, Vitalii Demianets vi...@nppfactor.kiev.ua

 wrote:



 So. to write compatible scripts one should check  0, not == -1.



Which matches the documentation:

Returns  0 if str1 is less than str2;  0 if str1 is greater than str2,
and 0 if they are equal.

David


David:

I think the point here is that the documentation says the function 
returns a number greater than zero, or less than zero, or zero.


It's just that many who have responded on this list show that the 
function commonly returns -1, 1, and 0.


It's wise to keep with the documentation.

Cheers,

tedd

--
---
http://sperling.com/

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



[PHP] strcmp()?

2011-05-23 Thread tedd

At 8:13 AM + 5/23/11, Ford, Mike wrote:

  -Original Message-
  From: tedd [mailto:tedd.sperl...@gmail.com]
  On Sat, 21 May 2011 09:26:02 -0400, tedd wrote:
The function strcmp() simply evaluates two strings and reports
  back -1, 0, or 1 depending upon their  alphabetical relationship.
  

 It might do that, but don't bet your horse on it.
 
 http://se.php.net/manual/en/function.strcmp.php
 

  /Nisse


 It works that way for me.


Are you absolutely certain about that?

   echo strcmp('These are nearly equal', 'These are almost equal'), \n;
   echo strcmp('different', 'unequal'), \n;
   echo strcmp('b', 'a'), br /\n;

Result:

   13
   -17
   1

The description of the function merely says that the result is 0, 0 or 0
-- it makes no promises about the actual value when it is non-zero.

Mike


Mike:

That's interesting. Try the same comparisons here:

http://www.webbytedd.com/lcc/citw229/string-compare.php

For me they are 1, -1, and 1.

Someone with more smarts than me* will have to figure this one out.

Cheers,

tedd

PS: * I can hear the peanut gallery saying That won't be hard.  :-)

--
---
http://sperling.com/

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



Re: [PHP] strcmp()?

2011-05-23 Thread Joshua Kehn
On May 23, 2011, at 8:00 AM, tedd wrote:

 At 8:13 AM + 5/23/11, Ford, Mike wrote:
  -Original Message-
  From: tedd [mailto:tedd.sperl...@gmail.com]
  On Sat, 21 May 2011 09:26:02 -0400, tedd wrote:
The function strcmp() simply evaluates two strings and reports
  back -1, 0, or 1 depending upon their  alphabetical relationship.
  
 It might do that, but don't bet your horse on it.
 
 http://se.php.net/manual/en/function.strcmp.php
 
  /Nisse
 
 It works that way for me.
 
 Are you absolutely certain about that?
 
   echo strcmp('These are nearly equal', 'These are almost equal'), \n;
   echo strcmp('different', 'unequal'), \n;
   echo strcmp('b', 'a'), br /\n;
 
 Result:
 
   13
   -17
   1
 
 The description of the function merely says that the result is 0, 0 or 0
 -- it makes no promises about the actual value when it is non-zero.
 
 Mike
 
 Mike:
 
 That's interesting. Try the same comparisons here:
 
 http://www.webbytedd.com/lcc/citw229/string-compare.php
 
 For me they are 1, -1, and 1.
 
 Someone with more smarts than me* will have to figure this one out.
 
 Cheers,
 
 tedd
 
 PS: * I can hear the peanut gallery saying That won't be hard.  :-)
 
 -- 
 ---
 http://sperling.com/

Might that have something to do with the version of PHP running? 

Regards,

-Josh

Joshua Kehn | josh.k...@gmail.com
http://joshuakehn.com


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



Re: [PHP] strcmp()?

2011-05-23 Thread tedd

  Mike:


 That's interesting. Try the same comparisons here:

 http://www.webbytedd.com/lcc/citw229/string-compare.php


  For me they are 1, -1, and 1.

Might that have something to do with the version of PHP running?


-Josh



-Josh:

I've written this on two different servers.

One is Version 5.2.15 and the other is version 5.2.5 and they both 
report the same results.


Cheers,

tedd

--
---
http://sperling.com/

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



Re: [PHP] strcmp()?

2011-05-23 Thread Joshua Kehn
On May 23, 2011, at 8:17 AM, tedd wrote:

  Mike:
 
 That's interesting. Try the same comparisons here:
 
 http://www.webbytedd.com/lcc/citw229/string-compare.php
 
  For me they are 1, -1, and 1.
 
 Might that have something to do with the version of PHP running?
 
 
 -Josh
 
 
 -Josh:
 
 I've written this on two different servers.
 
 One is Version 5.2.15 and the other is version 5.2.5 and they both report the 
 same results.
 
 Cheers,
 
 tedd


I just checked under 5.3.2 and it gives the same -1, 0, 1 results. 

Regards,

-Josh

Joshua Kehn | josh.k...@gmail.com
http://joshuakehn.com


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



RE: [PHP] strcmp()?

2011-05-23 Thread Jay Blanchard
[snip][/snip]

5.2.9 yields -1, 0, 1

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



RE: [PHP] strcmp()?

2011-05-23 Thread Ford, Mike
 -Original Message-
 From: Joshua Kehn [mailto:josh.k...@gmail.com]
 Sent: 23 May 2011 13:04
 
 On May 23, 2011, at 8:00 AM, tedd wrote:
 
  At 8:13 AM + 5/23/11, Ford, Mike wrote:

echo strcmp('These are nearly equal', 'These are almost
 equal'), \n;
echo strcmp('different', 'unequal'), \n;
echo strcmp('b', 'a'), br /\n;
 
  Result:
 
13
-17
1
 
  The description of the function merely says that the result is
 0, 0 or 0
  -- it makes no promises about the actual value when it is non-
 zero.
 
  Mike
 
  Mike:
 
  That's interesting. Try the same comparisons here:
 
  http://www.webbytedd.com/lcc/citw229/string-compare.php
 
  For me they are 1, -1, and 1.
 
  Someone with more smarts than me* will have to figure this one
 out.
 
  Cheers,
 
  tedd
 
  PS: * I can hear the peanut gallery saying That won't be hard.
 :-)
 
  --
  ---
  http://sperling.com/
 
 Might that have something to do with the version of PHP running?

Possibly -- or even the result returned by the underlying C strcmp()
for any given architecture/compiler combination, which would like as
not be even more variable.

I think the lesson is, if writing portable code, always allow for
results which might be outside of the [-1, 0, 1] set.

(Incidentally, tedd, your test script has the   signs the wrong way
round in the output; plus which they should be lt; gt; anyway; plus
plus which, you are not applying htmlspecialchars() or whatever to
your echoed user input, so values such as

   !--

break your page, and I'm sure something more malicious could be cooked
up were I so inclined... :( .)

Cheers!

Mike

 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507 City Campus, 
Portland Way, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.f...@leedsmet.ac.uk 
Tel: +44 113 812 4730





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] strcmp()?

2011-05-23 Thread Richard Quadling
On 23 May 2011 13:24, Joshua Kehn josh.k...@gmail.com wrote:
 On May 23, 2011, at 8:17 AM, tedd wrote:

  Mike:

 That's interesting. Try the same comparisons here:

 http://www.webbytedd.com/lcc/citw229/string-compare.php

  For me they are 1, -1, and 1.

 Might that have something to do with the version of PHP running?


 -Josh


 -Josh:

 I've written this on two different servers.

 One is Version 5.2.15 and the other is version 5.2.5 and they both report 
 the same results.

 Cheers,

 tedd


 I just checked under 5.3.2 and it gives the same -1, 0, 1 results.

 Regards,

 -Josh
 
 Joshua Kehn | josh.k...@gmail.com
 http://joshuakehn.com


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



Just tested the code below on Windows, using official releases (and
some RCs also).

?php
echo
PHP_VERSION, ' ',
strcmp('These are nearly equal', 'These are almost equal'), ' ',
strcmp('different', 'unequal'), ' ',
strcmp('b', 'a');
?

And for all of the V4 and V5 releases I've got, the result is the same...

-1 1 -1


-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

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



Re: [PHP] strcmp()?

2011-05-23 Thread Alexey Bovanenko
I checked on php 5.2.4-2 (ubuntu5.12). It returns 1,-1,1

On Mon, May 23, 2011 at 4:00 PM, tedd t...@sperling.com wrote:

 At 8:13 AM + 5/23/11, Ford, Mike wrote:

   -Original Message-
   From: tedd [mailto:tedd.sperl...@gmail.com]
   On Sat, 21 May 2011 09:26:02 -0400, tedd wrote:
 The function strcmp() simply evaluates two strings and reports
   back -1, 0, or 1 depending upon their  alphabetical relationship.
   

  It might do that, but don't bet your horse on it.
  
  http://se.php.net/manual/en/function.strcmp.php
  

   /Nisse


  It works that way for me.


 Are you absolutely certain about that?

   echo strcmp('These are nearly equal', 'These are almost equal'), \n;
   echo strcmp('different', 'unequal'), \n;
   echo strcmp('b', 'a'), br /\n;

 Result:

   13
   -17
   1

 The description of the function merely says that the result is 0, 0 or 0
 -- it makes no promises about the actual value when it is non-zero.

 Mike


 Mike:

 That's interesting. Try the same comparisons here:

 http://www.webbytedd.com/lcc/citw229/string-compare.php

 For me they are 1, -1, and 1.

 Someone with more smarts than me* will have to figure this one out.

 Cheers,

 tedd

 PS: * I can hear the peanut gallery saying That won't be hard.  :-)

 --
 ---
 http://sperling.com/

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




-- 
With regards,
Alexei Bovanenko


Re: [PHP] strcmp()?

2011-05-23 Thread Joshua Kehn

On May 23, 2011, at 9:28 AM, Alex Nikitin wrote:

 There is an interesting note in the comments for strcmp:
 Well, I am using PHP 4.0 and both strcmp and strcasecmp appear to be giving 
 me very arbitrary and incomprehensible results. When I input strings, it 
 appears that equal strings return 1, as well as some unequal strings, and 
 that if the first argument is smaller then I *tend* to get negative 
 numbers, but sometimes I get 1, and if larger I *tend* to get numbers larger 
 than 1.. 
 
 
 Guessing that earlier versions of php 4 and before would give the results 
 that would have values other then 1, 0, -1, i looked through the change log, 
 but nothing immediately jumped out, there was a lot of mbstring work done, 
 and they did add the nat comparison functions, and play with the pcre engine 
 a bit, which could have caused this as an unintended result for a few 
 versions, i think though it was a bug at some point, so, maybe a php dev 
 would chime in if they remember...?
 
 
 -- Alex --
 --
 The trouble with programmers is that you can never tell what a programmer is 
 doing until it’s too late.  ~Seymour Cray


All this confusion makes me glad that I'm using === for equality checks instead 
of strcmp. 

Regards,

-Josh

Joshua Kehn | josh.k...@gmail.com
http://joshuakehn.com


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



RE: [PHP] strcmp()?

2011-05-23 Thread tedd

At 1:06 PM + 5/23/11, Ford, Mike wrote:


(Incidentally, tedd, your test script has the   signs the wrong way
round in the output; plus which they should be lt; gt; anyway; plus
plus which, you are not applying htmlspecialchars() or whatever to
your echoed user input, so values such as

   !--

break your page, and I'm sure something more malicious could be cooked
up were I so inclined... :( .)

Mike


Mike:

Thanks.

The which way the arrows point thing is because I'm dyslexic. While 
I know that a appears before b, it's difficult for me to think of 
'a' being less than 'b' -- UNLESS -- I think in terms of their ASCII 
values and then everything makes sense -- but that's a step away from 
deciding  or . IOW, it's a two step process for me to realize which 
way the arrows point.


As for the htmlspecialchars(), you are absolutely right. The demo was 
for my students and I didn't want to confuse them. However, I have 
changed the code to htmlentities(). They probably should start 
learning basic security from the get-go (as should I).


Cheers,

tedd

--
---
http://sperling.com/

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



Re: [PHP] strcmp()?

2011-05-23 Thread tedd

At 9:32 AM -0400 5/23/11, Joshua Kehn wrote:


All this confusion makes me glad that I'm using === for equality 
checks instead of strcmp.


-Josh


-Josh:

Yes, but what if you were sorting? I know you could use sort(), but 
there might be logic where a strcmp() would better solve the problem.


Cheers,

tedd
--
---
http://sperling.com/

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



Re: [PHP] strcmp()?

2011-05-23 Thread Alex Nikitin
On Mon, May 23, 2011 at 9:32 AM, Joshua Kehn josh.k...@gmail.com wrote:


 On May 23, 2011, at 9:28 AM, Alex Nikitin wrote:

  There is an interesting note in the comments for strcmp:
  Well, I am using PHP 4.0 and both strcmp and strcasecmp appear to be
 giving me very arbitrary and incomprehensible results. When I input strings,
 it appears that equal strings return 1, as well as some unequal strings,
 and that if the first argument is smaller then I *tend* to get negative
 numbers, but sometimes I get 1, and if larger I *tend* to get numbers larger
 than 1.. 
 
 
  Guessing that earlier versions of php 4 and before would give the results
 that would have values other then 1, 0, -1, i looked through the change log,
 but nothing immediately jumped out, there was a lot of mbstring work done,
 and they did add the nat comparison functions, and play with the pcre engine
 a bit, which could have caused this as an unintended result for a few
 versions, i think though it was a bug at some point, so, maybe a php dev
 would chime in if they remember...?
 
 
  -- Alex --
  --
  The trouble with programmers is that you can never tell what a programmer
 is doing until it’s too late.  ~Seymour Cray


 All this confusion makes me glad that I'm using === for equality checks
 instead of strcmp.

 Regards,

 -Josh
 
 Joshua Kehn | josh.k...@gmail.com
 http://joshuakehn.com


It depends on what you need to check, josh :)

If you wanted to say find an anagram, or do a search with some typo
correction, strcmp can be many times more helpful then a ===, that said
comparing 2 strings to be equal === works about 20% quicker, so it works
better for comparing two strings for equality (or unequality) anyways. There
is no confusion, strcmp has a documented way in which it is to work in
posix-compliant languages, ISO/IEC 9899:1999, 7.21.4.2, so as long as you
follow the ISO guidelines for the scrcmp checking, your code should work
correctly...

--
The trouble with programmers is that you can never tell what a programmer is
doing until it’s too late.  ~Seymour Cray


RE: [PHP] strcmp()?

2011-05-23 Thread Ford, Mike
 -Original Message-
 From: tedd [mailto:tedd.sperl...@gmail.com]
 Sent: 23 May 2011 14:41


 The which way the arrows point thing is because I'm dyslexic.
 While
 I know that a appears before b, it's difficult for me to think
 of
 'a' being less than 'b' -- UNLESS -- I think in terms of their ASCII
 values and then everything makes sense -- but that's a step away
 from
 deciding  or . IOW, it's a two step process for me to realize
 which
 way the arrows point.

Yes, I remember you mentioning being dyslexic a few times on this list
before, which is partly why it was only an incidentally at the end.
We have a pretty hot disability and dyslexia unit here who don't
shrink from telling me what's good and what's bad about our website!

And, just for the record re the strcmp() debate, I'm on PHP 5.2.5,
SunOS 5.10.

Cheers!

Mike
 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507 City Campus, 
Portland Way, LEEDS,  LS1 3HE,  United Kingdom 
E: m.f...@leedsmet.ac.uk T: +44 113 812 4730




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] strcmp()?

2011-05-23 Thread Joshua Kehn
On May 23, 2011, at 9:45 AM, tedd wrote:

 At 9:32 AM -0400 5/23/11, Joshua Kehn wrote:
 
 All this confusion makes me glad that I'm using === for equality checks 
 instead of strcmp.
 
 -Josh
 
 -Josh:
 
 Yes, but what if you were sorting? I know you could use sort(), but there 
 might be logic where a strcmp() would better solve the problem.
 
 Cheers,
 
 tedd
 -- 
 ---
 http://sperling.com/

Never encountered an issue using sort() as-is. 

Regards,

-Josh

Joshua Kehn | josh.k...@gmail.com
http://joshuakehn.com


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



Re: [PHP] strcmp()?

2011-05-23 Thread Joshua Kehn
On May 23, 2011, at 9:47 AM, Alex Nikitin wrote:
  
 It depends on what you need to check, josh :)
 
 If you wanted to say find an anagram, or do a search with some typo 
 correction, strcmp can be many times more helpful then a ===, that said 
 comparing 2 strings to be equal === works about 20% quicker, so it works 
 better for comparing two strings for equality (or unequality) anyways. There 
 is no confusion, strcmp has a documented way in which it is to work in 
 posix-compliant languages, ISO/IEC 9899:1999, 7.21.4.2, so as long as you 
 follow the ISO guidelines for the scrcmp checking, your code should work 
 correctly...
 
 --
 The trouble with programmers is that you can never tell what a programmer is 
 doing until it’s too late.  ~Seymour Cray


It's good to know it's functionality is available in the case that I ever need 
it. 

Regards,

-Josh

Joshua Kehn | josh.k...@gmail.com
http://joshuakehn.com


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



Re: [PHP] strcmp()?

2011-05-23 Thread Richard Quadling
On 23 May 2011 14:28, Alex Nikitin niks...@gmail.com wrote:
 There is an interesting note in the comments for strcmp:
 Well, I am using PHP 4.0 and both strcmp and strcasecmp appear to be giving
 me very arbitrary and incomprehensible results. When I input strings, it
 appears that equal strings return 1, as well as some unequal strings,
 and that if the first argument is smaller then I *tend* to get negative
 numbers, but sometimes I get 1, and if larger I *tend* to get numbers larger
 than 1.. 

 Guessing that earlier versions of php 4 and before would give the results
 that would have values other then 1, 0, -1, i looked through the change log,
 but nothing immediately jumped out, there was a lot of mbstring work done,
 and they did add the nat comparison functions, and play with the pcre engine
 a bit, which could have caused this as an unintended result for a few
 versions, i think though it was a bug at some point, so, maybe a php dev
 would chime in if they remember...?

4.0.0 1 -1 1
4.0.1 1 -1 1
4.0.1 1 -1 1
4.0.2 1 -1 1
4.0.3 1 -1 1
4.0.4 1 -1 1
4.0.4pl1 1 -1 1
4.0.5 1 -1 1
4.0.6 1 -1 1
4.1.0 1 -1 1
4.1.1 1 -1 1
4.1.2 1 -1 1
4.2.0 1 -1 1
4.2.1 1 -1 1
4.2.2 1 -1 1
4.2.3RC1 1 -1 1
4.2.3RC2 1 -1 1
4.2.3 1 -1 1
4.3.0-pre2 1 -1 1
4.3.0RC1 1 -1 1
4.3.0RC2 1 -1 1
4.3.0RC3 1 -1 1
4.3.0RC4 1 -1 1
4.3.0 1 -1 1
4.3.1 1 -1 1
4.3.10 1 -1 1
4.3.11 1 -1 1
4.3.2-RC1 1 -1 1
4.3.2-RC2 1 -1 1
4.3.2RC3 1 -1 1
4.3.2 1 -1 1
4.3.3RC1 1 -1 1
4.3.3RC2 1 -1 1
4.3.3RC3 1 -1 1
4.3.3RC4 1 -1 1
4.3.3 1 -1 1
4.3.4RC1 1 -1 1
4.3.4RC2 1 -1 1
4.3.4RC3 1 -1 1
4.3.4 1 -1 1
4.3.5RC1 1 -1 1
4.3.5RC2 1 -1 1
4.3.5RC3 1 -1 1
4.3.5RC4 1 -1 1
4.3.5 1 -1 1
4.3.6RC1 1 -1 1
4.3.6RC2 1 -1 1
4.3.6RC3 1 -1 1
4.3.6 1 -1 1
4.3.7RC1 1 -1 1
4.3.7 1 -1 1
4.3.8 1 -1 1
4.3.9RC1 1 -1 1
4.3.9 1 -1 1
4.4.0 1 -1 1
4.4.1 1 -1 1
4.4.2 1 -1 1
4.4.3 1 -1 1
4.4.4 1 -1 1
4.4.5 1 -1 1
4.4.6 1 -1 1
4.4.7 1 -1 1
4.4.8 1 -1 1
4.4.9 1 -1 1

All the official versions of PHP 4 (and some RCs) for Windows.

All give the same response.

Must me a platform issue also.
-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

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



[PHP] strcmp() versus ==

2009-03-16 Thread Paul M Foster
I had never completely read over the rules with regard to comparisons in
PHP, and was recently alarmed to find that $str1 == $str2 might not
compare the way I thought they would. Is it common practice among PHP
coders to use strcmp() instead of == in making string comparisons? Or am
I missing/misreading something?

Paul

-- 
Paul M. Foster

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



[PHP] strcmp($var1, $var2) versus if ($var1 $var2)

2008-05-28 Thread C.R.Vegelin
Hi All,

I must be overlooking something here ...

$var1 = 01011090; $var2 = 010190; // 2 strings
if ($var1  $var2) ECHO var1  var2; else ECHO var1 = var2; echo br /;
$r = strcmp ( $var1 , $var2 );
if ($r  0) ECHO var1  var2, br /;

2nd line says: $var1 = $var2
4th line says: $var1  $var2

TIA, Cor

Re: [PHP] strcmp($var1, $var2) versus if ($var1 $var2)

2008-05-28 Thread David Otton
2008/5/28 C.R.Vegelin [EMAIL PROTECTED]:

 $var1 = 01011090; $var2 = 010190; // 2 strings
 if ($var1  $var2) ECHO var1  var2; else ECHO var1 = var2; echo br 
 /;
 $r = strcmp ( $var1 , $var2 );
 if ($r  0) ECHO var1  var2, br /;

 2nd line says: $var1 = $var2
 4th line says: $var1  $var2

Implicit type conversion.  is a numeric operator, so your strings
are silently promoted to integers, where (1011090  10190).

strcmp() treats the strings as strings, and orders them in something
close to ASCII order (which isn't the same as alphabetical ordering,
BTW, and see the comments at www.php.net/strcmp for locale-specific
gotchas).

PHP's implicit conversions can bite you if you don't understand them.
Try this one:

$a = 'string';
$b = 0;
if ($a==true  $b==false  $a==$b)
{
echo ('universe broken');
}

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



Re: [PHP] strcmp($var1, $var2) versus if ($var1 $var2)

2008-05-28 Thread C.R.Vegelin
- Original Message - 
From: David Otton [EMAIL PROTECTED]

To: C.R.Vegelin [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED] php-general@lists.php.net
Sent: Wednesday, May 28, 2008 12:11 PM
Subject: Re: [PHP] strcmp($var1, $var2) versus if ($var1  $var2)



2008/5/28 C.R.Vegelin [EMAIL PROTECTED]:


$var1 = 01011090; $var2 = 010190; // 2 strings
if ($var1  $var2) ECHO var1  var2; else ECHO var1 = var2; echo 
br /;

$r = strcmp ( $var1 , $var2 );
if ($r  0) ECHO var1  var2, br /;

2nd line says: $var1 = $var2
4th line says: $var1  $var2


Implicit type conversion.  is a numeric operator, so your strings
are silently promoted to integers, where (1011090  10190).

strcmp() treats the strings as strings, and orders them in something
close to ASCII order (which isn't the same as alphabetical ordering,
BTW, and see the comments at www.php.net/strcmp for locale-specific
gotchas).

PHP's implicit conversions can bite you if you don't understand them.
Try this one:

$a = 'string';
$b = 0;
if ($a==true  $b==false  $a==$b)
{
   echo ('universe broken');
}



Hi David,
I already suspected that the  operator forced a numeric comparison.
Even if I use: if ((string) $var1  (string) $var2) ...
So I have to use strcmp()
Thanks, Cor





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