Differences and usage between PHPecho, print, printf, and sprintf functions

Source: Internet
Author: User
This article mainly analyzes the differences and usage between echo, print, printf, and sprintf functions in PHP. if you need some help, please refer to them. 1. echo function:

The output function is a command and cannot return values. Echo can be followed by multiple parameters separated by semicolons, for example:
Echo $ myvar1;
Echo 1, 2, $ myvar ,"Bold";


2. print function:

Yes. one value can be returned and only one parameter is allowed.

Int print (string arg)

Outputs arg. Returns 1, always.


3. printf function:

Int printf (string format [, mixed args [, mixed...])

Produces output according to format, which is described in the documentation for sprintf ().

Returns the length of the outputted string.


Format the text and output it, for example:
$ Name = "hunte ";
$ Age = 25;
Printf ("my name is % s, age % d", $ name, $ age );


4. sprintf function:
String sprintf (string format [, mixed args [, mixed...])

Returns a string produced according to the formatting string format.


Similar to printf, but not printed, the formatted text is returned. Others are the same as printf.


5. the printf () function is described in detail:

The call format of the printf () function is:
Printf (" <格式化字符串> ", <参量表> );


% D decimal signed integer
% U decimal unsigned integer
% F floating point number
% S string
% C single character
% P pointer value
% E exponential floating point number
% X, % X unsigned integer in hexadecimal format
% O unsigned integer in octal format
% G automatically selects the appropriate notation


Note:

(1). you can insert a number between "%" and a letter to indicate the largest field width.

① For example, % 3d indicates that three integer values are output, and the three digits are not right aligned.

② % 9.2f indicates the floating point number with the output field width of 9. the decimal point is 2, the integer is 6, and the decimal point occupies one place. if the number is not 9, it is right aligned.

③ % 8 s indicates the string with 8 characters output, which is not 8 characters right aligned.

④ If the length of a string, or the number of integer digits exceeds the field width, the string is output based on the actual length.

⑤ Floating point number. if the integer part of the number of digits exceeds the width of the specified integer part, it is output according to the actual integer;

⑥ If the decimal point width exceeds the specified decimal point width, the output is rounded to the specified width.

7. if you want to add some 0 before the output value, add 0 before the field width.

For example, % 04d indicates that when a value smaller than four digits is output, the total width of the value is set to 4 by adding 0 in front.

If a floating point is used to represent the output format of a character or integer, the number after the decimal point represents the maximum width, and the number before the decimal point represents the minimum width.

For example, % 6.9 s indicates that a string with a length of not less than 6 and not greater than 9 is displayed. If the value is greater than 9, content after 9th characters will be deleted.


(2) you can add the lower-case letter l between "%" and the letter to indicate that the output is a long number.

① For example, % ld indicates the output of a long integer

② % Lf indicates that the double floating point number is output.


(3) you can control the left or right alignment of the output. adding a "-" sign between "%" and letters indicates that the output is left alignment. Otherwise, the output is right alignment.

① For example, %-7d indicates the left alignment of the output 7-digit integer

② %-10 s indicates that the output is left aligned with 10 characters


(4). Special characters

①/N line feed
②/F clear the screen and change the page
③/R press enter
④/T Tab character
⑤/Xhh indicates an ASCII code in hexadecimal notation,
6. hh indicates one to two hexadecimal numbers.

6. printf (): examples

Example 1: varous examples
The code is as follows:
$ N = 43951789;
$ U =-43951789;
$ C = 65; // ASCII 65 is 'A'

// Notice the double %, this prints a literal '% 'character
Printf ("% B = '% B'/n", $ n); // binary representation
Printf ("% c = '% c'/n", $ c); // print the ascii character, same as chr () function
Printf ("% d = '% d'/n", $ n); // standard integer representation
Printf ("% e = '% e'/n", $ n); // scientific notation
Printf ("% u = '% u'/n", $ n); // unsigned integer representation of a positive integer
Printf ("% u = '% u'/n", $ u); // unsigned integer representation of a negative integer
Printf ("% f = '% f'/n", $ n); // floating point representation
Printf ("% o = '% O'/n", $ n); // octal representation
Printf ("% s = '% s'/n", $ n); // string representation
Printf ("% x = '% X'/n", $ n); // hexadecimal representation (lower-case)
Printf ("% X = '% X'/n", $ n); // hexadecimal representation (upper-case)

Printf ("% + d = '% + D'/n", $ n); // sign specifier on a positive integer
Printf ("% + d = '% + D'/n", $ u); // sign specifier on a negative integer
?>



The printout of this program wocould be:
% B = '000000'
% C = 'A'
% D = '000000'
% E = '4. 39518e + 7'
% U = '000000'
% U = '000000'
% F = '2014. 43951789'
% O = '000000'
% S = '000000'
% X = '29ea6ad'
% X = '29ea6ad'
% + D = '+ 100'
% + D = '-43951789'

Example 2: string specifiers
The code is as follows:
$ S = 'monkey ';
$ T = 'Your monkeys ';

Printf ("[% s]/n", $ s); // standard string output
Printf ("[% 10 s]/n", $ s); // right-justification with spaces
Printf ("[%-10 s]/n", $ s); // left-justification with spaces
Printf ("[% 010 s]/n", $ s); // zero-padding works on strings too
Printf ("[% '#10 s]/n", $ s); // use the custom padding character '#'
Printf ("[% 10.10 s]/n", $ t); // left-justification but with a cutoff of 10 characters
?>

The printout of this program wocould be:
[Monkey]
[Monkey]
[Monkey]
[0000 monkey]
[#### Monkey]
[Mongomonke]

Example 3: zero-padded integers
The code is as follows:
$ Isodate = sprintf ("% 04d-% 02d-% 02d", $ year, $ month, $ day );
?>

Example 4: formatting currency
The code is as follows:
$ Money1 = 68.75;
$ Money2 = 54.35;
$ Money = $ money1 + $ money2;
// Echo $ money will output "123.1 ";
$ Formatted = sprintf ("% 01.2f", $ money );
// Echo $ formatted will output "123.10"
?>

Example 5: sprintf (): scientific notation
The code is as follows:
$ Number = 362525200;

Echo sprintf ("%. 3e", $ number); // outputs 3.63e + 8
?>

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.