PHP printf() Function
The printf() function in PHP outputs a formatted string.
The related functions of printf() function are as follows:
- sprintf()
- vprintf()
- vsprintf()
- fprintf()
- vfprintf()
Syntax
1 2 3 |
printf ( string $format [, mixed $... ] ) |
Parameter
format(required)- This parameter formats the string which is composed of zero or more directives where the ordinary characters (excluding %) that are copied directly to the result and conversion specifications.
The possible format values are as follows:
- %% – Returns a percent sign
- %b – Binary number
- %c – The character according to the ASCII value
- %d – Signed decimal number (negative, zero or positive)
- %e – Scientific notation using a lowercase (e.g. 1.2e+2)
- %E – Scientific notation using a uppercase (e.g. 1.2E+2)
- %u – Unsigned decimal number (equal to or greather than zero)
- %f – Floating-point number (local settings aware)
- %F – Floating-point number (not local settings aware)
- %g – shorter of %e and %f
- %G – shorter of %E and %f
- %o – Octal number
- %s – String
- %x – Hexadecimal number (lowercase letters)
- %X – Hexadecimal number (uppercase letters)
Return
This function returns the length of the outputted string.
Example 1
1 2 3 4 5 6 7 8 |
<?php // initializing the number $number = 123423; // printing the number by printf() function printf("%f",$number); ?> |
Output
1 2 3 |
123423.000000 |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php //initializing the positive integer $num1 = 123456789; //initializing negative integer $num2 = -123456789; // passign a character value $char = 50; // The ASCII Character 50 is 2 echo "The binary value for ".$num1." is: "; printf("%b",$num1); // Binary number echo "\nThe ASCII character value for ".$char." is: "; printf("%c",$char); // The ASCII Character echo "\nThe signed decimal value for ".$num2." is: "; printf("%d",$num1); // Signed decimal number ?> |
Output
1 2 3 4 5 |
The binary value for 123456789 is: 111010110111100110100010101 The ASCII character value for 50 is: 2 The signed decimal value for -123456789 is: 123456789 |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php //initializing the positive integer $num1 = 92345678; //initializing negative integer $num2 = -92345678; echo "The octal value for ".$num1." is: "; printf("%o",$num1); // Octal number echo "\nThe string value for ".$num1." is: "; printf("%s",$num1); // String echo "\nThe hexadecimal number in lowercase for ".$num2." is: "; printf("%x",$num2); // Hexadecimal number (lowercase) echo "\nThe hexadecimal number in uppercase for ".$num2." is: "; printf("%X ",$num2); // Hexadecimal number (uppercase) ?> |
Output
1 2 3 4 5 6 |
The octal value for 92345678 is: 540212516 The string value for 92345678 is: 92345678 The hexadecimal number in lowercase for -92345678 is: fffffffffa7eeab2 The hexadecimal number in uppercase for -92345678 is: FFFFFFFFFA7EEAB2 |
Example 4
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $str1 = "Hello PHP world"; // demonstrating the string specifiers printf("\n%s",$str1); printf("\n%8s",$str1); printf("\n%-8s",$str1); printf("\n%08s",$str1); printf("\n%'*8s",$str1); printf("\n%8.8s",$str1); ?> |
Output
1 2 3 4 5 6 7 8 |
Hello PHP world Hello PHP world Hello PHP world Hello PHP world Hello PHP world Hello PH |