Ugluth Posted May 16, 2008 Share Posted May 16, 2008 Hello there, this is my first post and let me start by saying that this is a really helpful and nice forum. Well let me get to the question. I'm reading a book to learn some basics of php and i've come across the printf function. The book said that to show a hex number u gotta use %x or %X. Anyway here's what i wrote: <?php $myString = "Blue"; $hex = 10; $formatted = printf ("My favorite color is %s and my lucky number is %x.", $myString, $hex); echo $formatted; ?> The result i get is "My favorite color is Blue and my lucky number is a.51" What is that 51 in the end? Sorry if this question is answered in another thread, but i looked a little bit and didnt find the answer. Quote Link to comment Share on other sites More sharing options...
kobmat Posted May 16, 2008 Share Posted May 16, 2008 try this <?php $myString = "Blue"; $hex = 10; printf ("My favorite color is %s and my lucky number is %x.", $myString, $hex); ?> printf echos the text to the screen and return the number of characters printed with in your case is 51, hence the 51 at the end of the sentence. if you really want the string to be in a variable use <?php $myString = "Blue"; $hex = 10; $formatted = sprintf ("My favorite color is %s and my lucky number is %x.", $myString, $hex); echo $formatted; ?> Quote Link to comment Share on other sites More sharing options...
Ugluth Posted May 16, 2008 Author Share Posted May 16, 2008 Thank you very much To be honest i didn't really want it to be stored in a variable, just thats the way the book i'm reading, had it in that form. Btw since i already posted, could actually do another question. This is the book i'm reading to learn some basic php stuff http://www.techotopia.com/index.php/PHP_Essentials, since i've only worked just a little bit with asp .net 2.0 and i'm kinda new to web development. I think its kinda outdated or something because in some cases examples it uses, i just try them out and they just don't work. If you have a better suggestion for a book to read to learn some stuff for php it would be more than welcome. For the info i've downloaded wamp2 to test out the examples (just in case that does play a role). Thanks again for your help Quote Link to comment Share on other sites More sharing options...
jonsjava Posted May 16, 2008 Share Posted May 16, 2008 Beginning PHP 5 and MySQL. Great book for novice and pro alike. Quote Link to comment Share on other sites More sharing options...
Ugluth Posted May 16, 2008 Author Share Posted May 16, 2008 Thanks a lot will get it! Quote Link to comment 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.