This article introduces echo, print, print_r, printf, sprintf, and var_dump. For more information, see. 1. define echo and use the PHPecho () function to output one or more string methods.
This article introduces 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 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.
The prompt is that the. echo () function is a little faster than the print () function.
Note: The echo () function can use the simplified syntax. see example 5.
The instance code is as follows:
-
- $ 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 instance 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.
The instance 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, starting with the percent sign ("%") to the end of the conversion character. below is the possible format value.
* %-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.
The instance 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 letterQuantity
The format parameter is the conversion format, starting with the percent sign ("%") to the end of the conversion character. the possible format values below.
%-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. placeholder after the % symbol, which consists of numbers and "$". See Example 3.
Tips: Related functions: fprintf (), printf (), vfprintf (), vprintf (), and vsprintf ().
The instance code is as follows:
-
- $ Str = "Hello ";
- $ Number = 123;
- $ Txt = sprintf ("% s world. Day number % u", $ str, $ number );
- Echo $ txt;
- ?>
Output.
Hello world. Day number 123
-
-
- $ Number = 123;
- $ Txt = sprintf ("% f", $ number );
- Echo $ txt;
- ?>
Output.
123.000000
-
- $ 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.
Prompt. to prevent the program from outputting the result directly 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.
We can compare var_dump () and print_r ().
The instance 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)
- */
- ?>
-