Deep Understanding of PHP printf () Output formatted strings, in-depth understanding of printf_php tutorials

Source: Internet
Author: User

In-depth understanding of PHP printf () Output formatted strings, in-depth understanding of printf


The PHP printf () function is used to output formatted strings, and this article introduces the use of the PHP printf () function and the basic usage examples of the code farm, which is of interest to the farmer.

Definition and usage

The printf () function outputs a formatted string.

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

Note: If the% symbol is more than the arg parameter, you must use a placeholder. Placeholders are inserted after the% symbol, consisting of numbers and "\$". See Example 2.

Tip: Related functions: sprintf (), vprintf (), vsprintf (), fprintf (), and vfprintf ()

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

Grammar

Parameters Description
Format

Necessary. Specifies the string and how to format the variable.

Possible format values:

  • Percent percent-returns a percent semicolon%
  • %b-Binary number
  • The character that corresponds to the%C-ASCII value
  • %d-decimal number with sign (negative, 0, positive)
  • %e-Using the lowercase scientific notation (e.g. 1.2e+2)
  • %E-Use uppercase scientific notation (e.g. 1.2E+2)
  • %u-decimal number with no sign (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-Eight binary number
  • %s-String
  • %x-16 binary number (lowercase letters)
  • %x-16 decimal digits (uppercase letters)

The attached format value. Must be placed between% and letter (e.g.%.2f):

  • + (precede the number with + or-to define the positive and negative of the number.) By default, only negative numbers are marked, positive numbers are not marked)
  • ' (specifies what to use as a fill, the default is a space.) It must be used with the width of the specified device. )
  • -(left adjustment variable value)
  • [0-9] (minimum width of the specified variable value)
  • . [0-9] (Specify the number of decimal digits or the maximum string length)

Note: If you use more than one of the above format values, they must be used in the order above and cannot be disrupted.

Arg1 Necessary. Specifies the parameter that is inserted into the first% symbol in the format string.
Arg2 Necessary. Specifies the parameter that is inserted into the second% symbol in the format string.
arg++ Optional. Specifies the parameter that is inserted into the format string third to fourth, and so on, in the% symbol.

Technical details

return value: Returns the length of the string being output.
PHP version: 4 +

Instance

Example 1

Use the format value%f:

Example 2

Use placeholders:

<?php $number = 123; printf ("There are two decimal places:%1\$.2f

Example 3

Demo of all possible format values:

<?php $num 1 = 123456789; $num 2 =-123456789; $char = 50; ASCII character 50 is 2  

















", $num 2); Symbol specifier (negative)?>

Example 4

A demo of the string specifier:

<?PHP$STR1 = "Hello"; $str 2 = "Hello world!"; printf ("[%s]
", $str 1);p rintf (" [%8s]
", $str 1);p rintf (" [%-8s]
", $str 1);p rintf (" [%08s]
", $str 1);p rintf (" [% ' *8s]
", $str 1);p rintf (" [%8.8s]
", $str 2);? >

This article in-depth understanding of PHP printf () output formatted string is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we have a lot of support to help guests home.

http://www.bkjia.com/PHPjc/1133103.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133103.html techarticle In- depth understanding of PHP printf () Output formatted strings, in-depth understanding of the printf PHP printf () function used to output formatted strings, this article to the Code Farm introduction PHP printf () function of the user ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.