Jump to content

PHP function issue


EchoFool

Recommended Posts

Hey

 

I have a simple function which isn't giving me the correct answer yet when i echo what is going on the numbers are correct but the answer is not... this is what i got:

 

<?php
Echo $FreeSpace.' divided by '.$Size.' = '.number_format(floor($FreeSpace/$Size)).'<br>';
?>

 

And this is what resulted:

 

249,100 divided by 1 = 249

 

Clearly not correct lol what did i do wrong?

Link to comment
https://forums.phpfreaks.com/topic/189653-php-function-issue/
Share on other sites

The reason is the comma separator in your variable $FreeSpace.  I don't know the specifics of why it's the case, my only guess is PHP sees it as a string rather than a numeric value (even though it does change the string "249,000" to 249).

 

If you remove the comma from $FreeSpace you'll find that it works fine.

Link to comment
https://forums.phpfreaks.com/topic/189653-php-function-issue/#findComment-1000933
Share on other sites

You coud do an not-so-elegant str_replace() on commas in $FreeSpace I guess - it would be better though if you did this when you were initialising the $FreeSpace variable.  Or if it's being pulled from a database, removing the comma before inserting the value to the DB.

Link to comment
https://forums.phpfreaks.com/topic/189653-php-function-issue/#findComment-1001184
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.