php

PHP Print() Function

PHP has many built-in functions to print the formatted output. The printf() function is one of them. This function is used to generate the formatted strings using one or more arguments and particular specifiers. The “%” symbol is used with each specifier. The uses of this function using different specifiers to generate the formatted output are shown in this tutorial.

Syntax:
int printf (string format [, mixed argument [, mixed …]] )

The first two arguments of this function are mandatory and the total number of arguments depends on the number of specifiers that are used in the first argument. The format string with the specifier is defined in the first argument. The second or other argument contains the variable based on the specifiers. It returns the length of the formatted string.

Formatting the Specifier

The different types of specifiers that are commonly used in the printf() function are described in the following:

Specifier Purpose
% It is used to print the percent character.
c It is used to print the character, ex: “a”.
s It is used to print the string, ex: “Hello”.
d It is used to print the decimal number, ex: 675, -54.
u It is used to print the unsigned decimal number, ex: 239.
f It is used to print the floating-point number, ex: 78.45.
e It is used to print the large fractional number, ex: 1.0E+3.
b It is used to print the binary number, ex: 0111011.
o It is used to print the octal number, ex: 0634.
x It is used to print the lowercase hexadecimal number, ex: 67bd.
X It is used to print the uppercase hexadecimal number, ex: A90D.

Different Examples of Printf() Function

The different uses of the printf() function are shown in this part of the tutorial using multiple examples.

Example 1: Print a Single Variable with Formatting

Create a PHP file with the following script that prints an integer variable with formatting using the printf() function. The “%d” specifier is used in the printf() function to print the integer value.

<?php

//Set the integer value
$price = 15;

//Set the format string
$format = "The price of pen is %d ";

//Print the integer value
printf($format, $price);

?>

Output:

The following output appears after executing the previous script:

Example 2: Print Multiple Variables with Formatting

Create a PHP file with the following script that prints the string and float variables with the formatting using the printf() function. The “%s” specifier is used to print the string variable and the “%f” specifier is used to print the float variable.

<?php

//Set the string value
$name = "Mouse";
//Set the integer value
$cost = 50.99;
//Set the format string
$format = "The price of %s is %f ";

//Print the integer value
printf($format, $name, $cost);

?>

Output:

The following output appears after executing the previous script. The number of digits after the decimal point can be set using the proper formatting with the “%f” specifier that is shown in the next example.

Example 3: Print a Number Using Different Formatting Specifiers

Create a PHP file with the following script that prints a number in different formats using the printf() function. The “%d” specifier is used to print the decimal number. The “%0.2f” specifier is used to print the floating point number with 2 digits after the decimal point. The “%b” specifier is used to print the binary number. The “%o” specifier is used to print the octal number. The “%x” specifier is used to print the hexadecimal number. The “%u” specifier is used to print the unsigned integer number.

<?php
//Set a number value
$number = 67.72;

//Print the output based on the specifier used in the printf() function
printf("The integer value = %d <br>", $number);
printf("The float value = %0.2f <br>", $number);
printf("The binary value = %b <br>", $number);
printf("The octal value = %o <br>", $number);
printf("The hexadecimal value = %x <br>", $number);
printf("The unsigned integer value = %u <br>", -$number);

?>

Output:

The following output appears after executing the previous script:

Example 4: Print the Character Based on the ASCII Code

Create a PHP file with the following script that prints the character of a particular ASCII code using the printf() function. The “%c” specifier is used to print the character of the ASCII value. The ASCII value of the character “F” is 70 which is used in the script.

<?php

//Define the ASCII value of F
$ascii_code = 70;

//Print the original string
printf("The character of ASCII code-<b>%d</b> is <b>%c</b> ", $ascii_code, $ascii_code);

?>

Output:

The following output appears after executing the previous script:

Example 5: Generate the Formatted String by Badding

Create a PHP file with the following script that prints the string by adding different types of padding with the string value and cut the string value using the printf() function. The %’*15s specifier is used here to print the string variable by adding “*” with the string on the left to make the string length 15. The %’*-15s specifier is used here to print the string variable by adding “*” with the string on the right to make the string length 15. The %.3s specifier is used here to print the string variable by cutting the first three characters of the string.

<?php

//Define a string value
$string = "Welcome";

//Print the original string
printf("The original string :<b> %s </b><br>", $string);
//Print string by padding '*' in the left
printf("Padding string with * in the left: <b> %'*15s </b><br/>", $string);
//Print string by padding '*' in the right
printf("Padding string with * in the right: <b> %'*-15s </b><br/>", $string);
//Print the first 3 characters of the string
printf("The string after cutting 3 characters :<b> %.3s </b><br/>", $string);

?>

Output:

The following output appears after executing the previous script:

Conclusion

The printf() function is a very useful function of PHP to print the output in different ways using the different types of specifiers. The uses of this function are cleared after reading this tutorial properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.