Common PHP variable output: echo, prinf, sprintf, var_dump

Source: Internet
Author: User
Common PHP variable output: echo, prinf, sprintf, var_dump,

1. use the echo statement
Echo can be used to print variables and content. other variables can be system variables, HTML code, or PHP expressions, as shown in the following example:
$ A = "12345"; // variable assignment
$ B = "this is string ";
// The following two variables are printed respectively.
Echo $;
Echo $ B;
// Display the content submitted by the form
Echo $ _ POST ['username'];
$ Str1 = "FREEBSD ";
$ Str2 = "PHP"; // HTML text
// Link strings $ str1 and $ str2 are displayed
Echo $ str1. "and". $ str2. "is good partners .";
?>
 
2. use the printf function
The printf function is used to format the output string. it is mainly used to replace the format string starting with % in the string.
Syntax: boolean printf (string format [, mixed args])
See the following example:
Printf ("$ % 01.2f", 43.2); // running result: $43.20
Printf ("% d bottles of beer on % s", 100, "the wall ");
// Running result: 100 bottles of beer on the wall
Printf ("% 15 s", "some text"); // running result: some text
?>
 
As you can see, if the string starting with % is displayed, the parameters are replaced in order. As follows:
Printf ("The % 2 \ $ s likes to % l \ $ s", "bark", "dog ");
// Running result: The dog likes to bark
Printf ("The % l \ $ s says: % 2 \ $ s, % 2 \ $ s.", "dog", "bark ");
// Running result: The dog says: bark, bar.
 
3. use the sprintf function
The sprintf function is also used for string formatting. This function is basically the same as the printf function, but it can save the converted result to a string variable rather than directly output it.
Syntax: string sprintf (string format, mixed [args]...);
The format parameter is the conversion format, starting with the percent sign % to the conversion character. See the following script example:
$ Var1 = 68.75;
$ Var2 = 54.35;
$ Var3 = $ var1 + $ var2;
// Variable $ var3 value is "123.1 ";
$ Formatted = sprintf ("% 01.2f", $ var3 );
// Variable $ var3 value is "123.10"
?>
Here, the % sign of % 01.2f is the start of the specified format, that is, starting from "starting character" until "conversion character" appears, and the formatting character is formally completed.
In %
The value 0 after the symbol indicates "fill in the blank character". if the position is null, it is filled with 0. After 0, 1 indicates that the number before the decimal point must have more than one place, replace 1 with 2. if the value of $ var3 is
1.23, the value of $ formatted will be 01.23. Since the number before the decimal point only occupies one digit, according to the format specified above, the number before the decimal point should occupy two digits, now only 1
Bits, so use 0 to fill. 2 After % 01 indicates the number after the decimal point, which must take two places. If $ money is 1.234, $ formatted
Is 1.23. Why is 4 missing? Because only two digits can be entered after the decimal point according to the preceding rules. However, the value of $ var3 occupies three decimal places, so 4 is removed, leaving only
23.
Finally, it ends with the f conversion character. for other conversion characters, refer to the character conversion list below.
 
Conversion character
% Print the percent sign without conversion
Integer B to binary
Convert an integer in C to an ASCII character
D integer to decimal
Convert F-precision numeric to floating point number
O integer to octal
S integer to string
Convert x/X integers to lowercase/uppercase hexadecimal numbers

 
If a minus sign is added after the starting symbol "%", the number is aligned to the right. As shown in the following example.
$ Money = 1.4;
$ Formatted = sprintf ("%-02.2f", $ money );
Echo $ formatted;
?>
At this time, $ formatted will not be 01.40 but 1.400
The conversion format is as follows:
1. fill in the blanks. If the value is 0, enter 0 as the space. if the value is 0, the default value is space.
2. alignment. The default value is the right alignment, and the negative sign indicates the left alignment.
3. field width. Minimum width.
4. accuracy. The number of floating point digits after the decimal point.
 
4. differences between echo and print statements
Echo is a statement that only displays text information or variable values, but does not return values;
Printf (including printf and sprintf) is a function that returns a boolean value (True or False );
The echo statement does not return values. However, you can use a. (dot) to concatenate strings, but print does not. If the display is simple, echo is more efficient (no return value ).

5. var_dump
Print information about variables,

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.
// String variable
$ A = "123 ";
Var_dump ($ );
Running result: string (3) "123"
Print the type, length, and value of the variable;


// Numeric quantity
$ B = 456;
Var_dump ($ B );
Running result: int (456)
Print the type and value of the variable;


// Array
$ C = array ('php start', 'phpqidian. com ');
Var_dump ($ c );
Running result: array (2 ){
[0] =>
String (9) "PHP"
[1] =>
String (13) "phpqidian.com"
}
Print the type, number of elements, and information corresponding to the elements of the array;


// Boolean variable
$ D = true;
Var_dump ($ d );
Running result: bool (true)

 

PHP small class training | PHP video tutorial | PHP Training

PHP Training in Tiantongyuan

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.