Introduction to echo, print, print_r, printf, sprintf, and var_dump in php _ PHP Tutorial

Source: Internet
Author: User
Php echo, print, print_r, printf, sprintf, var_dump usage introduction. This article introduces echo, print, print_r, printf, sprintf, and var_dump. For more information, see. I. definition and usage of echo: The PHPecho () function outputs one or more strings. the following articles describe echo, print, print_r, printf, sprintf, and var_dump. For more information, see.

1. echo
Definition and usage
The PHP echo () function outputs one or more strings.

Echo "" This method can also be used without brackets.

Syntax
Echo (strings)
Parameter description
Strings is required. One or more strings to be sent to the output.

Tips and comments
Note: echo () is actually not a function, so you do not need to use parentheses for it. However, if you want to pass one or more parameters to echo (), a parsing error occurs when brackets are used.

Tip: The echo () function is a little faster than the print () function.

Tip: The echo () function can use the simplified syntax. See example 5.

The code is as follows:

Example
Example 1
$ Str = "Who's John Adams? ";
Echo $ str;
Echo"
";
Echo $ str ."
I don't know! ";
?>

Output:

Who's John Adam?
Who's John Adam?
I don't know!

Example 2
Echo "This text spans multiple lines .";
?>

Output:

This text spans multiple lines.

Example 3
Echo 'this', 'string', 'was', 'Made ', 'With multiple parameters ';
?>

Output:

This string was made with multiple parameters

Example 4
The difference between single quotes and double quotes. Single quotes only output variable names, not values:

$ Color = "red ";
Echo "Roses are $ color"; echo"
";
Echo 'roses are $ color';?>

Output:

Roses are red Roses are $ color

Example 5
Simplified syntax:

$ Color = "red ";
?>

Roses are

  


II. print
Print () is the same as echo (), but the echo speed is a little faster than print. In fact, it is not a function, so you do not need to use parentheses for it. However, if you want to pass more than one parameter to print (), a parsing error occurs when brackets are used. Note that print always returns 1, which is different from echo, that is, print can be used to assign values, but it has no practical significance.
Example:

The code is as follows:
$ A = print ("55nav"); // This is allowed
Echo $ a; // The value of $ a is 1.
?>

III. print_r functions
The print_r function prints easy-to-understand information about variables.
Syntax: mixed print_r (mixed $ expression [, bool return])
If the variable is string, integer or float, its value is directly output. if the variable is an array, a formatted array is output for ease of reading, that is, the format corresponding to the key and value. Object objects are similar. Print_r has two parameters. The first parameter is a variable, and the second parameter can be set to true. if it is set to true, a string is returned. Otherwise, a Boolean value is returned.
Example:

The code is as follows:

$ A = "55nav ";
$ C = print_r ($ );
Echo $ c; // The value of $ c is TRUE.
$ C = print_r ($ a, ture );
Echo $ c; // The value of $ c is the string 55nav.
?>

IV. printf functions
The printf function returns a formatted string.
Syntax: printf (format, arg1, arg2, arg ++)
The format parameter is the conversion format. it starts with the percent sign ("%") and ends with the conversion character. The following are possible format values:
* %-Percentage sign returned
* % B-binary number
* % C-ASCII characters
* % D-signed decimal number
* % E-resumable counting (for example, 1.5e + 3)
* % U-unsigned decimal number
* % F-floating point number (local settings aware)
* % F-floating point number (not local settings aware)
* % O-octal values
* % S-string
* % X-hexadecimal (lowercase letter)
* % X-hexadecimal number (uppercase letters)
Parameters such as arg1, arg2, arg ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. in the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on. If the % symbol is greater than the arg parameter, you must use a placeholder. After the placeholder is inserted with the % symbol, it consists of numbers and "$. You can use numbers to specify the displayed parameters. For more information, see the example.
Example:

The code is as follows:

Printf ("My name is % s. "," 55nav "," com "); // My name is 55nav com.
Printf ("My name is % 1 $ s % 1 $ s", "55nav", "com "); // add 1 $ or 2 $ ..... the position where the following parameters are displayed. this line outputs My name is Ricky because only the first parameter is displayed twice.
Printf ("My name is % 2 $ s % 1 $ s", "55nav", "com"); // My name is com 55nav
?>

5. function/reschedule HTM target = _ blank> sprintf function
The format parameter is the conversion format. it starts with the percent sign ("%") and ends with the conversion character. The following possible format values:

%-Percentage sign returned

% B-binary number

% C-characters based on ASCII values

% D-signed decimal number

% E-scientific notation (for example, 1.5e + 3)

% U-unsigned decimal number

% F-floating point number (local settings aware)

% F-floating point number (not local settings aware)

% O-octal characters % s-string

% X-hexadecimal (lowercase letter)

% X-hexadecimal (uppercase letters)

Parameters such as arg1, arg2, ++ are inserted to the percent sign (%) in the main string. This function is executed step by step. In the first % symbol, insert arg1, at the second % symbol, insert arg2, and so on.

Tips and comments
Note: If the % symbol is greater than the arg parameter, you must use a placeholder. The placeholder is inserted after the % symbol, which consists of numbers and "$. See Example 3.

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

The code is as follows:

Example
Example 1
$ Str = "Hello ";
$ Number = 123;
$ Txt = sprintf ("% s world. Day number % u", $ str, $ number );
Echo $ txt;
?>

Output:

Hello world. Day number 123

Example 2
$ Number = 123;
$ Txt = sprintf ("% f", $ number );
Echo $ txt;
?>

Output:

123.000000

Example 3
$ Number = 123;
$ Txt = sprintf ("With 2 decimals: % 1 $. 2f
With no decimals: % 1 $ u ", $ number );
Echo $ txt;
?>

Output:

With 2 decimals: 123.00 With no decimals: 123

PHP String function


VI. var_dump function
Var_dump (PHP 3> = 3.0.5, PHP 4, PHP 5)

Var_dump -- print information about a variable

Void var_dump (mixed expression [, mixed expression [,...])

This function displays the structure information about one or more expressions, including the expression type and value. The array recursively expands the value and displays its structure through indentation.

Tip: to prevent the program from directly outputting the results to the browser, you can use the output-control function to capture the output of this function, and save them to a variable of the string type, for example.


You can compare var_dump () with print_r ().


Example

The code is as follows:


  

  

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

Var_dump ($ );

/* Output:

Array (3 ){

[0] =>

Int (1)

[1] =>

Int (2)

[2] =>

Array (3 ){

[0] =>

String (1) ""

[1] =>

String (1) "B"

[2] =>

String (1) "c"

}

}

*/

$ B = 3.1;

$ C = TRUE;

Var_dump ($ B, $ c );

/* Output:

Float (3.1)

Bool (true)

*/

?>

  

Bytes. I. echo definition and usage: the PHP echo () function outputs one or more strings...

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.