How to print a formatted string in PHP

Overview

A string is a data type that could be alphanumeric, numeric or alphabetical, but is enclosed in a single or double quote. Strings are displayed on the screen using regular output commands in PHP.

A formatted string is an interpolated string literal where placeholders are used in place of an actual value and are replaced during the evaluation of such strings with the value. It contains values that will be converted to the indicated type during evaluation.

Below is a sample formatted string.

$formstr = "There are %u really good persons %f out there %s.";

The printf() function in PHP is used to print formatted strings.

How to use printf() to output a formatted string

First, let’s take a look at the syntax and other vital information about this function.

Syntax

printf($format,$var1, $var2,.....$varN);

Parameters

  • format: This is a required parameter that specifies the string and how the variables it contains should be formatted. These formats have the percent (%) symbol placed before them.

In PHP the following format values are used:

  1. %% : A percent % sign will be returned.
  2. %b : This will return a binary number.
  3. %c : This changes ASCII values into characters.
  4. %d : It returns a signed decimal number (negative, zero or positive).
  5. %e : It indicates scientific notation using lowercase (i.e., 1.2e+2).
  6. %E : It indicates scientific notation using uppercase (i.e., 1.2E+2)
  7. %E : This is a decimal number that is unsigned (that is >= 0).
  8. %f : This is a floating-point number.
  9. %s : This formats as a string.

And more.

  • var1,var2....varN: These are the set of variables whose values will be formatted. It could be just one or as many as you want. If there are many of these variables, they should be placed sequentially in the order of appearance of their placeholders and conversion format in the $format parameter.

Return value

This function has a return value of the outputted string’s length.

Code

<?php
$int = 4782;
$anotherint = -4782;
$char = 57; // The ASCII Character 57 is 9
// Note: The format value "%%" returns a percent sign
// Binary number
printf("%%b = %b <br>",$int);
// The ASCII Character
printf("%%c = %c ASCII Character converter<br>",$char);
// Signed decimal number
printf("%%d = %d is a Signed decimal number<br>",$anotherInt);
// Scientific notation (uppercase)
printf("%%E = %E is the Scientific notation of 4782 <br>",$int);
// Unsigned decimal number (positive)
printf("%%u = %u <br>",$int);
// Unsigned decimal number (negative)
printf("%%u = %u <br>",$anotherint);
// Floating-point number (local settings aware)
printf("%%f = %f <br>",$int);
// A string with converters inside it.
printf("These %1\$.2f are formatted strings %u examples. <br>",$int,$anotherint);
?>

Explanation

In the code above, different formatting styles were tried to show how they work. Take a close look at the inline comments to see what happened on each code line.

Meanwhile, on the last code line in the snippet, the \$.2f format was used to set the floating point number to just two decimal places. In this format, \$.2f, the slash was used to escape the dollar sign, which indicates that whatever is after it is an extra formatting option.

Free Resources