Jump to content

[SOLVED] Displaying hex with php


Ugluth

Recommended Posts

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.

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

Thank you very much  :D 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  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.