RE: strcmp()?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Joshua Kehn [mailto:josh.kehn@xxxxxxxxx]
> Sent: 23 May 2011 13:04
> 
> On May 23, 2011, at 8:00 AM, tedd wrote:
> 
> > At 8:13 AM +0000 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.ford@xxxxxxxxxxxxxx 
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




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux