ghurty Posted January 25, 2010 Share Posted January 25, 2010 I have two variables that I am adding to each other $time = $num1+$num2; Sometimes $num1 will have a leading zero (ex: 0410) but it will always be four digits long. The way it is now, if there is a leading zero, it outputs $time as a three digit number without the leading zero. How can I have it that it will output it as a four digit number? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/189764-when-doing-addition-how-to-have-it-output-a-leading-zero/ Share on other sites More sharing options...
taquitosensei Posted January 25, 2010 Share Posted January 25, 2010 use sprintf to format it echo sprintf("%04d",$time); Quote Link to comment https://forums.phpfreaks.com/topic/189764-when-doing-addition-how-to-have-it-output-a-leading-zero/#findComment-1001443 Share on other sites More sharing options...
ghurty Posted January 25, 2010 Author Share Posted January 25, 2010 Thank you, but I am trying to display the number, I am using it for a file name. How can I have it assign that value to a variable? Quote Link to comment https://forums.phpfreaks.com/topic/189764-when-doing-addition-how-to-have-it-output-a-leading-zero/#findComment-1001459 Share on other sites More sharing options...
Mchl Posted January 25, 2010 Share Posted January 25, 2010 *Mchl rolls eyes $variable = sprintf("%04d",$time); Quote Link to comment https://forums.phpfreaks.com/topic/189764-when-doing-addition-how-to-have-it-output-a-leading-zero/#findComment-1001472 Share on other sites More sharing options...
ghurty Posted January 25, 2010 Author Share Posted January 25, 2010 Thank you. I had googled, but all the examples of sprintf had to do with displaying results on screen. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/189764-when-doing-addition-how-to-have-it-output-a-leading-zero/#findComment-1001474 Share on other sites More sharing options...
Mchl Posted January 25, 2010 Share Posted January 25, 2010 Well... manual clearly says it returns string. string sprintf ( string $format [, mixed $args [, mixed $... ]] ) Always check function's signature in manual, and you'll save yourself a lot of time Quote Link to comment https://forums.phpfreaks.com/topic/189764-when-doing-addition-how-to-have-it-output-a-leading-zero/#findComment-1001480 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.