The difference and usage of PHP echo,print,printf,sprintf functions _php Tips

Source: Internet
Author: User
Tags clear screen mixed sprintf string format

1. Echo Function:

The output function, which is a command, cannot return a value. Echo can be followed by many parameters, separated by semicolons, such as:
echo $myvar 1;
Echo 1,2, $myvar, "<b>bold</b>";


2. Print function:

is a function that can return a value and can have only one argument.

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 are described in the documentation for SPRINTF ().

Returns the length of the outputted string.


Format the text after the output, such as:
$name = "Hunte";
$age = 25;
printf ("My name was%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 does not print, but returns the formatted text, the rest is the same as printf.


5. Detailed explanation of printf () function:

The call format for the printf () function is:
printf ("< format string >", < Parameter table >);


%d decimal signed integer
%u decimal unsigned integer
%f floating-point numbers
%s string
%c single character
The value of the%p pointer
Floating point numbers in the form of%e exponent
%x, int without symbol in hexadecimal notation
%o An integer that is unsigned as octal
%g automatic selection of appropriate notation


Description

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

① Example:%3d indicates output 3-bit integer, not 3-bit right-aligned.

②%9.2F represents a floating-point number with an output field width of 9, where the decimal digit is 2, the integer digit is 6, the dot is one digit, and the 9-bit right alignment is not enough.

③%8s represents a 8-character string that is not 8 words Fu Yi aligned.

④ if the length of the string, or the number of integer digits exceeds the field width of the description, it will be output at its actual length.

⑤ floating-point number, if the integer partial digit exceeds the indicated integer digit width, will output according to the actual integer digit;

⑥ the fractional number of digits exceeds the indicated decimal width, the output is rounded by the width of the description.

⑦ If you want to add some 0 to the output value, you should add a 0 to the front width.

For example,%04d indicates that when outputting a value less than 4 digits, the front complement of 0 will make the total width 4 bits.

⑧ If you use floating-point numbers to represent the output format for characters or integers, the digits after the decimal point represent the maximum width, and the digits before the decimal point represent the minimum width.

For example,%6.9s represents a string that is not less than 6 and not greater than 9. If greater than 9, the contents of the 9th character will be deleted.


(2). You can add a lowercase letter L between "%" and a letter to indicate the length of the output.

① Example:%ld for output long integers

②%LF indicates output double floating-point numbers


(3). You can control the output left or right alignment, that is, add a "-" sign between "%" and the letter to indicate that the output is left-aligned or right-aligned.

① Example:%-7d indicates output 7-bit integer left-aligned

②%-10s indicates output 10 characters left-aligns alignment


(4). Some special specified characters

①/n Line Change
②/f Clear screen and change page
③/R Carriage return
④/t Tab character
⑤/XHH represents an ASCII code with a 16-in representation,
⑥ where HH is 1 to 2 16 in number

6. printf (): examples

Case 1:various Examples

Copy Code code as follows:

<?php
$n = 43951789;
$u =-43951789;
$c = 65; ASCII 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 would is:
%b = ' 10100111101010011010101101 '
%c = ' A '
%d = ' 43951789 '
%e = ' 4.39518e+7 '
%u = ' 43951789 '
%u = ' 4251015507 '
%f = ' 43951789.000000 '
%o = ' 247523255 '
%s = ' 43951789 '
%x = ' 29EA6AD '
%x = ' 29EA6AD '
%+d = ' +43951789 '
%+d = '-43951789 '

Case 2:string Specifiers
Copy Code code as follows:

<?php
$s = ' monkey ';
$t = ' many monkeys ';

printf ("[%s]/n", $s); Standard string output
printf ("[%10s]/n", $s); Right-justification with spaces
printf ("[%-10s]/n", $s); Left-justification with spaces
printf ("[%010s]/n", $s); Zero-padding works on strings too
printf ("[% ' #10s]/n", $s); Use the custom padding character ' # '
printf ("[%10.10s]/n", $t); Left-justification but with a cutoff of characters
?>

The printout of this program would is:
[Monkey]
[Monkey]
[Monkey]
[0000monkey]
[### #monkey]
[Many Monke]

Case 3:zero-padded Integers
Copy Code code as follows:

<?php
$isodate = sprintf ("%04d-%02d-%02d", $year, $month, $day);
?>

Case 4:formatting Currency
Copy Code code as follows:

<?php
$money 1 = 68.75;
$money 2 = 54.35;
$money = $money 1 + $money 2;
echo $money would output "123.1";
$formatted = sprintf ("%01.2f", $money);
echo $formatted would output "123.10"
?>

Example 5:sprintf (): Scientific notation
Copy Code code as follows:

<?php
$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.