PHP printf() Function

PHP printf() Function

The printf() function in PHP outputs a formatted string.

The related functions of printf() function are as follows:

  • sprintf()
  • vprintf()
  • vsprintf()
  • fprintf()
  • vfprintf()

Syntax

printf ( string $format [, mixed $... ] ) 

Parameter

format(required)- This parameter formats the string which is composed of zero or more directives where the ordinary characters (excluding %) that are copied directly to the result and conversion specifications.

The possible format values are as follows:

  • %% - Returns a percent sign
  • %b - Binary number
  • %c - The character according to the ASCII value
  • %d - Signed decimal number (negative, zero or positive)
  • %e - Scientific notation using a lowercase (e.g. 1.2e+2)
  • %E - Scientific notation using a uppercase (e.g. 1.2E+2)
  • %u - Unsigned decimal number (equal to or greather than zero)
  • %f - Floating-point number (local settings aware)
  • %F - Floating-point number (not local settings aware)
  • %g - shorter of %e and %f
  • %G - shorter of %E and %f
  • %o - Octal number
  • %s - String
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (uppercase letters)

Return

This function returns the length of the outputted string.

Example 1

 

Output

123423.000000

Example 2

 

Output

The binary value for 123456789 is: 111010110111100110100010101
The ASCII character value for 50 is:  2
The signed decimal value for -123456789 is:  123456789 

Example 3

 

Output

The octal value for 92345678 is: 540212516
The string value for 92345678 is:  92345678
The hexadecimal number in lowercase for -92345678 is:  fffffffffa7eeab2
The hexadecimal number in uppercase for -92345678 is:  FFFFFFFFFA7EEAB2  

Example 4

 

Output

Hello PHP world
Hello PHP world
Hello PHP world
Hello PHP world
Hello PHP world
Hello PH