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? 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 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? 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 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. Link to comment https://forums.phpfreaks.com/topic/258264-printf-query/#findComment-1323902 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.