Current Location: Home> Function Categories> printf

printf

Output formatted string
Name:printf
Category:String
Programming Language:php
One-line Description:Output a formatted string.

Definition and usage

printf() function outputs a formatted string.

The arg1 , arg2 , arg++ parameters will be inserted at the percent sign (%) symbol in the main string. This function is executed step by step. At the first % symbol, insert arg1 , at the second % symbol, insert arg2 , and so on.

Note: If the % symbol is more than the arg parameter, you must use placeholders. The placeholder is inserted into the % symbol and consists of a number and "\$". See Example 2.

Related functions:

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

grammar

 printf ( format , arg1 , arg2 , arg ++ )
parameter describe
format

Required. Specifies the string and how to format the variables therein.

Possible format values:

  • %% - Returns a percent sign
  • %b - binary number
  • %c - Characters corresponding to ASCII value
  • %d - Decimal number containing positive and negative signs (negative number, 0, positive number)
  • %e - Use lowercase scientific notation (e.g. 1.2e+2)
  • %E - Scientific notation using capitals (e.g. 1.2E+2)
  • %u - Decimal number without signs (greater than or equal to 0)
  • %f - Floating point number (local setting)
  • %F - Floating point number (non-local setting)
  • %g - shorter %e and %f
  • %G - Shorter %E and %f
  • %o - octal number
  • %s - string
  • %x - Hexadecimal number (lowercase letters)
  • %X - Hexadecimal number (caps)

Additional format value. Necessarily placed between % and letters (for example %.2f):

  • + (Present + or - before the number to define the positive and negative nature of the number. By default, only negative numbers are marked, and positive numbers are not marked)
  • ' (Specify what to use as padding, default is a space. It must be used with the width specifier.)
  • - (Left adjustment variable value)
  • [0-9] (Specify the minimum width of the variable value)
  • .[0-9] (Specify the number of decimal places or maximum string length)

Note: If you use multiple format values ​​above, they must be used in the order above and cannot be disrupted.

arg1 Required. Specifies the parameters inserted into the first % symbol in the format string.
arg2 Required. Specifies the parameter inserted into the second % symbol in the format string.
arg++ Optional. Specifies the parameters inserted into the third, fourth, etc. % symbols in the format string.
Similar Functions
  • Inversely escape an escaped string stripslashes

    stripslashes

    Inverselyescapeanesc
  • Get the string length strlen

    strlen

    Getthestringlength
  • One-way string hash crypt

    crypt

    One-waystringhash
  • Remove spaces (or other characters) from the beginning of the string ltrim

    ltrim

    Removespaces(orother
  • Convert the first byte of the string to a value between 0-255 ord

    ord

    Convertthefirstbyteo
  • Find the first occurrence of substrings in strings strpos

    strpos

    Findthefirstoccurren
  • Escape strings using backslash addslashes

    addslashes

    Escapestringsusingba
  • Output a string print

    print

    Outputastring
Popular Articles