Making 0-9 display as 00-09 ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Making 0-9 display as 00-09 ?

Post by Pyrite »

The subject says it all. Looked through the PHP manual, couldn't find any functions really. If I do something like if $foo <10, $foo = "0".$foo; it still shows up without the 0 in front.

Also had a question about tabs.
if I echo some and put a \t in it, it puts the tab in the code when I view source, but not when echoed. Is there a HTML tag for tab or a php way to tab. echoing " " doesn't do the trick.

Thanks!
User avatar
Leviathan
Forum Commoner
Posts: 36
Joined: Tue Sep 23, 2003 7:00 pm
Location: Waterloo, ON (Currently in Vancouver, BC)

Post by Leviathan »

Just like C/C++:

Code: Select all

printf("%02d", $foo);
%d means print a decimal; putting 02 means 2-digit 0-padded. Check the documentation for sprintf if you want to find out more about format strings.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

That did it! Thank you. I would have thought of that if I had ever used C, but I've only had some C++ and we used cout :?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

About tabs - if you need tabs to appear you either have to replace them with no-breaking whitespaces, use CSS to add a margin to the text or show the text within <pre> tags. Personally, I'd probably go the CSS route, but it does depend on what needs to be displayed.

Mac
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Well, I tried doing the <pre> tags thing, that didn't work though for me. Instead I put the &nbsp; things in the quotes with the printf function and that did it. I just basically wanted to display what I was printf'ing over a tab or two.
Post Reply