version_compare strangeness

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Echilon
Forum Newbie
Posts: 12
Joined: Fri Nov 25, 2005 4:11 am
Location: England
Contact:

version_compare strangeness

Post by Echilon »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm having problems getting version_compare to work. I have a constant ([b]IZ_MINVER[/b]) and a variable ([b]$_SESSION['backupmeta']['izver'][/b]). They're both in the format #.#.#, but version compare either always evaluates to true, or always to true, regardless of what version numbers are fed into it. I've tried so many variations, does anyone have any idea why this isn't working?

Code: Select all

echo IZ_MINVER.':'.$_SESSION['backupmeta']['izver'].':'.(version_compare((string)$_SESSION['backupmeta']['izver'],(string)IZ_MINVER,"lt") ? 'true':'false');
          if(version_compare((string)$_SESSION['backupmeta']['izver'],(string)IZ_MINVER,"lt")) {
// this is executed
} else {
//this should be executed, because the versions are equal.
}
Produces the output '2.8.8:2.8.8 :true'. But it should return 0, since they're equal.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Does it product the same results without the operator?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's a space in the session variable making them not equal.
User avatar
Echilon
Forum Newbie
Posts: 12
Joined: Fri Nov 25, 2005 4:11 am
Location: England
Contact:

Post by Echilon »

feyd wrote:There's a space in the session variable making them not equal.
Good eyesight, this had been illuding me for ages, then someone on IRC suggested trimming the strings.

Thanks for the help. :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I don't see a space anywhere.

+1 @ feyd if you knew from past experience that this was the cause

-1 @ the op for posting inaccurate data "'2.8.8:2.8.8 :true'"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

astions wrote:I don't see a space anywhere.

+1 @ feyd if you knew from past experience that this was the cause

-1 @ the op for posting inaccurate data "'2.8.8:2.8.8 :true'"
I knew from past experience that any variance, printable or not, will cause a difference in comparisons. Since the quoted output had a space in it and the code wouldn't have added a space the logical conclusion was that a space was in the string.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Yeah, but what number am I thinking of?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Kieran Huggins wrote:Yeah, but what number am I thinking of?
44?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

ZOMG!

It seems that feyd has some competition for this year's "most psychic" award!
Post Reply