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
https://forums.phpfreaks.com/topic/105953-solved-displaying-hex-with-php/
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;

?>

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  :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.