我有一个PHP变量,我必须显示一个3位数字的长度,右对齐[关闭]

this is a really noob question I guess, but I am very new to php and have no idea how to even begin this one, basically I have a set of $vars that I have to display the length of as a 3 digit number that is right justified.

Any pointers as to how to do this?

Many thanks for all your help.

This should take care of what you just described in your comment:

echo str_pad(strlen("hello"), 3 , "0", STR_PAD_LEFT);

This will return

005

Your answer lies within PHP documentation: http://php.net/manual/en/function.str-pad.php

From their example:

<?php
$input = "Alien";
echo str_pad($input, 10);                      // produces "Alien     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"
echo str_pad($input, 6 , "___");               // produces "Alien_"
?>

The printf version:

printf("%40d", 123); //                                      123

40 chars right justified