php

PHP Sprint() Function

The sprint() function of PHP works like the printf() function. Both functions generate the formatted output, but one difference between these functions is that the output of the sprinf() function is stored into a variable, and the output of the printf() function is displayed in the browser. How sprint() function works and the uses of this function is shown in this tutorial.

Syntax:

sprintf(string $format, mixed ...$values): string

The first argument of this function contains the format string that will be applied to the output. The second argument contains the value to which the formatting will be applied. This function returns the formatted string as the output on success but does not return false on failure for PHP version 8.

Specifiers

Many types of specifiers are used in the format string of this function. The list of mostly used specifiers has mentioned below.

Specifier Purpose
c It is used to print the character of an ASCII code.
d It is used to print integer numbers.
f It is used to print floating-point numbers.
s It is used to print string values.
e It is used to print numbers in exponential format.
b It is used to print binary numbers.
o It is used to print octal numbers.
u It is used to print unsigned decimal numbers.
x It is used to print hexadecimal numbers.
X It is used to print hexadecimal numbers using capital letter.

Pre-requisite

The scripts used in the examples of this tutorial are written based on the PHP 8 version. Do the following task before executing the examples of this script.

  1. Install apache2 and PHP 8.
  2. Set execute permission for all files and folders under /var/www/html folder where all PHP files will be stored.

Different Uses of Sprint() Function

The uses of sprint() function in PHP is shown in this part of the tutorial by using different examples.

Example-1: Use of different types of specifier in the format string

Create a PHP file with the following script to know the use of four different types of specifiers for formatting data using sprint() function. Four types of data have assigned in four variables in the script. Next, the values of these variables have printed by using sprint() function.

<?php

//Set an integer value

$integer = 300;

//Set a floating-point number

$float = 45.895;

//Set a string value

$string = 'LinuxHint';

//Set an ASCII value

$ascii = 65;


//Print the integer value

echo"The integer value is <b>".sprintf("%d",$integer)."</b><br/>";

//Print the fractional value

echo"The fractional value is <b>".sprintf("%0.2f",$float)."</b><br/>";

//Print the string value

echo"The string value is <b>".sprintf("%s",$string)."</b><br/>";

//Print the character of the ASCII code

echo"The character of ascii code <b>$ascii</b> is <b>".sprintf("%c",$ascii)."</b>";

?>

Output:

The following output will appear after executing the above script. Here, the filename is sprintf1.php that is stored inside /var/www/html/code folder.

http://localhost/code/sprintf1.php

Example-2: Use of scientific notation

Create a PHP file with the following script to know the way of generating formatted output with the scientific notation by using the sprint() function. In the example, a large number is assigned to a variable that will be printed in exponential format.

<?php

//Set a large numeric value

$large_num = 56565656565656;

/*

Format the large number with scientific notation

and store into a variable

*/


$expo = sprintf("%.4e", $large_num);

//Print the formatted value

echo "The formatted value of $large_num with exponential is $expo";

?>

Output:

The following output will appear after executing the above script. Here, the filename is sprintf2.php that is stored inside /var/www/html/code folder.

http://localhost/code/sprintf2.php

Example-3: Using string specifier in different ways

Create a PHP file with the following script to know the use of different string specifiers for generating different types of formatted output by using the sprint() function. In the example, a string value has been assigned to a variable. The first sprintf() function will print the main string. The spaces has used for left padding in the second sprinf() function without <pre> tag.

The spaces has used for left padding in the third sprinf() function with <pre> tag. The spaces has used for right padding in the fourth sprinf() function with <pre> tag. The zero has been used for left padding in the fifth sprinf() function. The ‘#’ character has been used for left padding in the sixth sprinf() function.

<?php

//Assign a text value

$text = 'Welcome to LinuxHint';

//Print the original text value

echo sprintf("%s",$text)."<br/>";

//Print the right-justified text value by padding with space

echo sprintf("%25s",$text)."<br/>";

//Print the right-justified text value with <pre> tag by padding with space

echo "<pre>". sprintf("%25s",$text)."</pre><br/>";

//Print the left-justified text value with <pre> tag and another text by padding with space

echo "<pre>". sprintf("%-25s",$text)."dummy text</pre><br/>";

//Print the text value by right-justifing with zero padding

echo sprintf("%025s",$text)."<br/>";

//Print the text value by right-justifing with the '#' padding

echo sprintf("%'#30s",$text)."<br/>";

?>

Output:

The following output will appear after executing the above script. Here, the filename is sprintf3.php that is stored inside /var/www/html/code folder. According to the output, space padding does not work without <pre> tag.

http://localhost/code/sprintf3.php

Example-4: Use of the positional specifier

Create a PHP file with the following script to know the way of using the sprint() function for the positional specifier. Here, two variables have initialized with two values. 1$ and 2$ have been used as the positional specifier in the sprintf() function.

<?php

//Set a string value

$product = "HP Laptop";

//Set the number value

$price = 1040;


//Ptint the formatted values with position specifier

echo sprintf('<br/><p>The price of this <b>%2$s</b> is <b>%1$d</b> dollars.</p>', $price, $product);

?>

<strong>Output:</strong>

The following output will appear after executing the above script. Here, the filename is <strong>sprintf4.php</strong> that is stored inside <strong>/var/www/html/code</strong> folder.

<a href="http://localhost/code/sprintf4.php">http://localhost/code/sprintf4.php</a>

<img class="wp-image-132476" src="https://linuxhint.com/wp-content/uploads/2021/11/word-image-161.png" />
<h2>Example-5: Format number with zero padding</h2>
Create a PHP file with the following script to know the way of formatting numbers with zero padding by using the <strong>sprint()</strong> function. Here, an integer number of three digits has been assigned into a variable. The original number and the number with zero padding will be printed after executing the code.
[cc lang="php" width="100%" height="100%" escaped="true" theme="blackboard" nowrap="0"]
<?php

//Assign a numeric value

$num = 785;

//Print the original number

echo "<br/>The original number value is <b>".sprintf("%d", $num)."</b><br/>";

//Print the number with zero padding

echo "The number value with zero padding is <b>".sprintf("%05d", $num)."</b><br/>";

?>

Output:

The following output will appear after executing the above script. Here, the filename is sprintf5.php that is stored inside /var/www/html/code folder.

http://localhost/code/sprintf5.php

Conclusion

The sprint() function is mainly used in PHP to store the formatted content of data into a variable. The uses of the most commonly used specifiers of this function have been explained in this tutorial to help the new PHP users to know the use of this function 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.