Jump to content

1.2 == 1.2, False?


MySQL_Narb

Recommended Posts

$version = '1.2';

 

Also, $latest also returns 1.2 (both should be strings)

 

Any idea why it still say you're outdated?

 


<?php
if(!isset($_GET['check_version'])){
echo 'You\'re running version '. $version .'; <a href="?check_version=11">Check if there\'s a new version.</a>';
}else{
$ch = curl_init('http://.../getLatest.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if(($latest = curl_exec($ch)) === false){
echo 'An error occured: '. curl_error($ch);
}else{
if($version != $latest){
echo 'You\'re outdated! <a href="http://.../">Download the new version</a>'. $latest;
}else{
echo 'You\'re running the latest version.';
}
}
curl_close($ch);
}
?>

Link to comment
Share on other sites

Looks like the age old problem of equalities with floats. One could really be 1.19999999999999 and the other 1.200000000001.

 

You could try echoing the values to check.

 

The best way to compare floats is to see if the difference is very small. eg

 

if ( abs($x - $y) < 0.00001) {
  // consider them equal
}

Link to comment
Share on other sites

array(2) { [0]=> string(157) "1.2 " [1]=> string(3) "1.2" }

 

See the numbers in parenthesis right after the word string? That is the length of the string. Your first string is 157 characters long, while your second is only 3 characters long. Obviously they do not match each other.

 

Your first string likely contains a lot of spaces padding it's value. You can use trim to remove these spaces.

Link to comment
Share on other sites

var_dump returns: array(2) { [0]=> string(157) "1.2 " [1]=> string(3) "1.2" }

 

There is a visible blank space between the first value and the closing double-quote. There is something else in that string. It also says it is 157 bytes long, so there is definitely something else there. If you dumped it in the browser, use the "view source" feature of your browser to see if there are non-rendering HTML values in there. You are going to have to figure out what is after the "2" and the strip it out.

Link to comment
Share on other sites

Always always always view the source when you var-dump.

 

That is the one thing I don't like about var_dump, there is no way to capture the output and send it through htmlspecialchars(). Unless I need the type and length, I generally use:

 

function debug($value) {
 printf('<PRE>%s</PRE>', htmpspecialchars(print_r($value, true)));
}
// Then 
debug($something);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.