PHP printf() Function


The printf() function displays a formatted string from one or more arguments.

Syntax
printf(format,arg1,arg2,arg++)
Parameters
format(Required) Each conversion specification starts with a single percent sign (%) and ends with the following conversion characters.

% - returns a percent sign.

b - the argument is an integer and display it as a binary number.

c - the argument is an integer and display it as a an ASCII value.

d - the argument is an integer and display as a signed decimal number.

e - the argument is scientific notation (e.g. 1.2e+2).

E - the argument as scientific notation (e.g.1.2E+2).

u - the argument is an integer, and display as an unsigned decimal number.

f- the argument is a float, and display as a floating-point number. (local aware)

F - the argument is a float, and display as a floating-point number (non-locale aware).

g - shorter of %e and %f.

G - shorter of %E and %f.

o- the argument is an integer, and display as an octal number.

s - the argument is string and display as a string.

x - the argument is an integer and display as a hexadecimal number (with lowercase letters).

X - the argument is an integer and display as a hexadecimal number (with uppercase letters).
arg1 (Required)The argument to be added the first %-sign in the formatted string
arg2,arg3 (optional)These arguments to be added the second%,third% etc. in the formatted string
Example
<?php
 $num = 100;
 printf("%d",$num);
?>

Output

100
Example
<?php
 $num = 100;
 printf("%f",$num);
?>

Output

100.00000
Example
<?php
  $stg = "Welcome";
  printf("Hi Hello ,%s to My home.",$stg);
?>

Output

Hi Hello, Welcome to My home.

Prev Next