whelpton Posted February 8, 2012 Share Posted February 8, 2012 Hey, just wondering what would be the best way of adding .1 to a string. For example: $newversion = $currentversion + '0.0.1'; $newversion is taken from a db, lets say for this example it is 0.2.1 This code simply outputs 0.2 Any ideas? Thanks =) Quote Link to comment https://forums.phpfreaks.com/topic/256647-php-incrementing-w-decimal-places/ Share on other sites More sharing options...
MasterACE14 Posted February 8, 2012 Share Posted February 8, 2012 $newversion = $currentversion . '0.0.1'; Quote Link to comment https://forums.phpfreaks.com/topic/256647-php-incrementing-w-decimal-places/#findComment-1315654 Share on other sites More sharing options...
whelpton Posted February 8, 2012 Author Share Posted February 8, 2012 Thank you, thats nearly got it. However now Im getting 0.2.101 or 0.2.10.1 if I use the original 0.0.1. Im just looking to increase the last decimal position by one. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/256647-php-incrementing-w-decimal-places/#findComment-1315656 Share on other sites More sharing options...
PFMaBiSmAd Posted February 8, 2012 Share Posted February 8, 2012 <?php function version_add($current,$add){ $current = explode('.',$current); $add = explode('.',$add); foreach($current as $key=>$value){ $current[$key] += $add[$key]; } return implode('.',$current); } $currentversion = '0.2.1'; $newversion = version_add($currentversion,'0.0.1'); echo $newversion; Quote Link to comment https://forums.phpfreaks.com/topic/256647-php-incrementing-w-decimal-places/#findComment-1315658 Share on other sites More sharing options...
MasterACE14 Posted February 8, 2012 Share Posted February 8, 2012 EDIT: PFMaBiSmAd beat me to it $currentversion = '0.2.1'; $v = explode('.', $currentversion); if($v[2] < 9) $v[2]++; elseif($v[1] < 9) { $v[1]++; $v[2] = 0; } elseif($v[0] < 9) { $v[0]++; $v[1] = 0; if($v[1] == 0) $v[2] = 0; } $newversion = implode('.',$v); echo $newversion; Quote Link to comment https://forums.phpfreaks.com/topic/256647-php-incrementing-w-decimal-places/#findComment-1315659 Share on other sites More sharing options...
whelpton Posted February 8, 2012 Author Share Posted February 8, 2012 Absoloutely fantastic. Thank you both! Quote Link to comment https://forums.phpfreaks.com/topic/256647-php-incrementing-w-decimal-places/#findComment-1315661 Share on other sites More sharing options...
MasterACE14 Posted February 8, 2012 Share Posted February 8, 2012 Just note if you have say... $currentversion = '1.0.9'; PFMaBiSmAd's code will return '1.0.10' My code will return '1.1.0' So whatever meets your needs. Cheers, Ace Quote Link to comment https://forums.phpfreaks.com/topic/256647-php-incrementing-w-decimal-places/#findComment-1315665 Share on other sites More sharing options...
PFMaBiSmAd Posted February 8, 2012 Share Posted February 8, 2012 For a software/hardware version number, '1.0.10' would be the expected result. You wouldn't increase the next higher significant field just because you have made 10 changes in a field with lower significance. Quote Link to comment https://forums.phpfreaks.com/topic/256647-php-incrementing-w-decimal-places/#findComment-1315716 Share on other sites More sharing options...
MasterACE14 Posted February 9, 2012 Share Posted February 9, 2012 ah true. Quote Link to comment https://forums.phpfreaks.com/topic/256647-php-incrementing-w-decimal-places/#findComment-1315995 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.