Naps Posted March 4, 2012 Share Posted March 4, 2012 Quick question regarding the printf function. I understand that printf will print a formatted string for example: $mystring = printf('%.1f', 1.2); //Will output 1.2 However I don't understand why if I then print $mystring the result is 3 Example: $mystring = printf('%.1f', 1.2); //prints 1.2 echo "<br><br>"; echo $mystring //Prints 3 Please could someone explain how this works? Quote Link to comment https://forums.phpfreaks.com/topic/258264-printf-query/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2012 Share Posted March 4, 2012 printf directly prints the output to the browser. It returns an integer that is - Return Values Returns the length of the outputted string. If you want to assign the formatted string to a variable, use sprintf Quote Link to comment https://forums.phpfreaks.com/topic/258264-printf-query/#findComment-1323851 Share on other sites More sharing options...
Naps Posted March 4, 2012 Author Share Posted March 4, 2012 Thanks PFMaBiSmAd, I am still curious though as to why when $mystring is echoed after $mystring = printf('%.1f', 1.2); it results in 3. Do you know why this is? Quote Link to comment https://forums.phpfreaks.com/topic/258264-printf-query/#findComment-1323881 Share on other sites More sharing options...
kicken Posted March 4, 2012 Share Posted March 4, 2012 Return Values Returns the length of the outputted string. The return value is the length of the generated string. "1.2" has length=3 Quote Link to comment https://forums.phpfreaks.com/topic/258264-printf-query/#findComment-1323894 Share on other sites More sharing options...
Naps Posted March 4, 2012 Author Share Posted March 4, 2012 Thanks guys, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/258264-printf-query/#findComment-1323902 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.