Summary of commonly used Output Functions in PHP-PHP source code

Source: Internet
Author: User
This article mainly introduces the summary of commonly used Output Functions in PHP. This article provides a brief summary of echo, print, die, printf, sprintf, print_r, and other functions, for more information about the output functions commonly used in PHP, see this article. This article provides a brief summary of echo, print, die, printf, sprintf, print_r, and other functions, for more information, see

Echo ();"Output Content ";

Multiple strings can be output at the same time, and multiple parameters are allowed. parentheses are not required and no return value is required.

Print ();A switch with a return value of 1 to 0 can only contain one parameter.

Only one string and one parameter can be output simultaneously. parentheses are required and return values. flase is returned when execution fails.
The usage of print is similar to that of C language, so it will make a special explanation of % in the output content.

The code is as follows:

$ A = print ('hi'); echo $ a; // ---------- hi 1 // 1 is the value of $. //----------

Die ();It is generally used to determine the database connection. Once die () is executed

Die (); // is different from exit.

There are two functions: output the content first, and then exit the program. (Commonly used in linking servers and databases)

The code is as follows:

Mysql_connect ("locahost", "root", "root") or die ("link server failed! ");

Printf ();Similar to the C language, the output can be formatted.

Printf (); // f indicates format

Printf ("parameter 1", parameter 2): parameter 1 = output in what format; parameter 2 = output variable.

(% S: by string; % d: by integer; % B: By binary; % x: by hexadecimal; % X: by hexadecimal; % x: by hexadecimal; % o: by octal; % f: by floating point)
Function, returns the number of output characters. after formatting the text, it is output, for example:

The code is as follows:

Printf ("$ % 01.2f", 43.2); // $43.20

$ Indicates the character to be filled
0 indicates that the number of digits is not enough and the original value is not affected.
1 indicates the total output width.
2 indicates the number of decimal places, rounded down
% F indicates a floating point number.

Formatting commands and instructions:

% Indicates the percentage, which is not converted.
% B integer to binary.
The % c integer is converted to the corresponding ASCII character.
% D integer to decimal place.
% F times the precision number to a floating point number.
% O integer to octal.
% S integer to string.
% X integer to lowercase hexadecimal.
% X integer to uppercase hexadecimal.

The code is as follows:

 

Sprintf;Store output content in variables

This cannot be output directly. assign a variable first and then output the variable.

The code is as follows:

 

Print_r ();Used to output arrays

Function: only outputs an array.

The code is as follows:

$ A = array (1, 2, array ("a", "B", "c "));
Print_r ($ );

Return value:

The code is as follows:

Array ([0] => 1 [1] => 2 [2] => Array ([0] => a [1] => B [2] => c))

Var_dump ();Can output any content

The content, type, and length of the output variable. It is often used for debugging.

The code is as follows:

 

Var_export ();
Returns the structure information about the variable passed to the function. it is similar to var_dump (). The difference is that the returned table is a valid PHP code.

You can return the value of a variable by setting the second parameter of the function to TRUE.

The code is as follows:

 1, 1 => 2, 2 => array (0 => 'A', 1 => 'B', 2 => 'C',),) */$ B = 3.1; $ v = var_export ($ B, TRUE); echo $ v;/* outputs: 3.1 */?>

Simplified usage:

The code is as follows:

 

Roses are

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.