String Formatting in PHP


If you want to display a string directly to the browser in its original state then PHP provides two functions that enable you to first apply to format. So in this article, you learn about "print()" and "sprintf()".

printf() Function

If you are familiar with the C programming language, then you are probably familiar with the printf() function. The printf() function requires a string argument, known as a format control string and it also accepts additional arguments of various types. The format control string contains the instructions for displaying the arguments. Now I am showing an example use of printf() to output an integer as an octal (or base-8) number.


Included within the format control string (the first argument) is a special code, known as a conversion specification. A conversion specification begins with a percent (%) symbol and defines how to treat the corresponding argument to printf(). You can include as many conversion specifications as you want within the format control string, as long as you send an equivalent number of arguments to printf().

printf("The octal number of integer $no is:%o",$no);




Type Specifiers of printf()

You have already seen specifiers (in other words "%o") to display a number as octal, the following table specifies more of the type specifiers:

Specifier Description
b It displays an integer as a binary number.
c It displays an integer as ASCII equivalent.
d It displays the argument as a decimal number.
f It displays an integer as a floating-point number.
o It displays an integer as an octal number.
s It displays the argument as a string.
x It displays an integer as a lowercase hexadecimal number (base 16).
X It displays an integer as an uppercase hexadecimal number (base 16).


Use the following code in a text file named "all.php". When you access this script through your web browser, it should look something like the following output. As you can see, printf() is a quick way of converting data from one number system to another and outputting the result.

printf("Binary: %b<br/>",$number);
printf("ASCII: %c<br/>",$number);
printf("Decimal: %d<br/>",$number);
printf("Float: %f<br/>",$number);
printf("Octal: %o<br/>",$number);
printf("Hexaadecimal(lower): %x<br/>",$number);
printf("Hexaadecimal(upper): %x",$number);



Although you can use the type specifier to convert from decimal to hexadecimal numbers, you cannot use it to determine how many characters the output for each argument should occupy.


The sprintf() function writes a formatted string to a variable.

The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.

Note: If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "\$". See example two.


$number = 123;
$txt = sprintf("%f",$number);
echo $txt;