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

Source: Internet
Author: User
Tags comments lowercase mixed sprintf

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: Copy code

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

Output:

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

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

Output:

This text spans multiple lines.

Example 3
<? Php
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:

<? Php
$ Color = "red ";
Echo "Roses are $ color"; echo "<br/> ";
Echo 'roses are $ color';?>

Output:

Roses are red Roses are $ color

Example 5
Simplified syntax:

<Html> <body> <? Php
$ Color = "red ";
?> <P> Roses are <? = $ Color?> </P> </body>


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: Copy code
<? Php
$ 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: Copy code

<? Php
$ 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: Copy code

<? Php
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: Copy code

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

Output:

Hello world. Day number 123

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

Output:

123.000000

Example 3
<? Php
$ Number = 123;
$ Txt = sprintf ("With 2 decimals: % 1 $. 2f <br/> 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: Copy code


<Pre>

<? Php

$ 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)

*/

?>

</Pre>

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.