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.
printf()
to output a formatted stringFirst, let’s take a look at the syntax and other vital information about this function.
printf($format,$var1, $var2,.....$varN);
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:
%%
: A percent %
sign will be returned.%b
: This will return a binary number.%c
: This changes ASCII values into characters.%d
: It returns a signed decimal number (negative, zero or positive).%e
: It indicates scientific notation using lowercase (i.e., 1.2e+2).%E
: It indicates scientific notation using uppercase (i.e., 1.2E+2)%E
: This is a decimal number that is unsigned (that is >= 0).%f
: This is a floating-point number.%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.This function has a return value of the outputted string’s length.
<?php$int = 4782;$anotherint = -4782;$char = 57; // The ASCII Character 57 is 9// Note: The format value "%%" returns a percent sign// Binary numberprintf("%%b = %b <br>",$int);// The ASCII Characterprintf("%%c = %c ASCII Character converter<br>",$char);// Signed decimal numberprintf("%%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);?>
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.