Common PHP variable output: Echo, Prinf, sprintf, Var_dump

Source: Internet
Author: User
Tags string format

1. Using the Echo statement
You can use Echo to print variables and content, others can be system variables, HTML code, or a PHP expression, such as the following example:
$a = "12345"; Assigning values to variables
$b = "This is a string";
The following print shows the contents of two variables respectively
echo $a;
Echo $b;
Display content submitted by a form
echo $_post[' UserName '];
$str 1 = "FREEBSD";
$str 2 = "PHP"; HTML-Text
Link strings $str1 and $str2 and display
echo $str 1. " and ". $str 2." is good partners. ";
?>

2. Use the printf function
The printf function is used to format the output string, which is used primarily to replace a format string that begins with% in a string.
Syntax: Boolean printf (String format[,mixed args])
Take a look at the following example:
printf ("$%01.2f", 43.2); Running Result: $43.20
printf ("%d bottles of beer on%s", "the Wall");
Operation result: Bottles of beer on the wall
printf ("%15s", "some text"); Run Result: some text
?>

You can see that the format string begins with%, and the parameter substitution is displayed sequentially. As shown below:
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. Using 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 instead of the direct output.
Syntax: string sprintf (string format, mixed [args] ...);
Where the parameters format is the converted format, starting with percent sign% to convert characters. See the following script example:
$var 1 = 68.75;
$var 2 = 54.35;
$var 3 = $var 1 + $var 2;
The variable $var3 value is "123.1";
$formatted = sprintf ("%01.2f", $var 3);
Variable $var 3 value of "123.10"
?>
Where:%01.2f's% symbol refers to the beginning of the fixed-type, that is, starting from the "starting character", until the "conversion character", the format of the character's work officially ended.
In
The symbol after 0 means "fill in the blanks", if the position is empty to fill with zero, in 0 after 1 specifies the number of digits in front of the decimal point to have more than 1 digits, 1, if the value of $VAR3
1.23, the value of $formatted will be 01.23. Since the number in front of the decimal point occupies only one person, according to the above-mentioned format, the number should be 2 digits before the decimal point, and now only 1
bit, so fill it with a full. Behind the%01.2 means that the number after the decimal point must be 2 digits. If the value of $money is 1.234, then $formatted
will have a value of 1.23. Why is the 4 gone? Because after the decimal point according to the above provisions, must and only can occupy 2 bits. However, the value of $VAR3 in the decimal point accounted for 3 bits, so 4 was removed, leaving only
23.
Finally, with the F conversion character ending, the other conversion characters refer to the following character conversion list.

Conversion character Function description
% print out percent symbol, do not convert
B int to binary number
C integers convert to ASCII characters
D Integer to Decimal
F-times precision numbers into floating-point number
O integer turns into octal number
S integer turns into a string
x/x integer converted to lowercase/uppercase hexadecimal number


If you add a-(minus sign) after the% start symbol, the numbers are handled in a right-aligned manner. This is 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 format of the conversion is once included as follows:
1, fill in the blanks characters. 0 words means blank space is 0, space is the default value.
2, the Alignment method. The default value is right-aligned and the minus sign is aligned to the left.
3, field width. To the minimum width.
4, accuracy. Refers to the number of floating-point digits after the decimal point.

4. About the difference between ECHO and print statements
Echo is a statement that simply displays text information or variable values without a return value;
printf (including printf, sprintf) is a function that can return a Boolean value (True or False);
The Echo statement does not return a value, but it can be used. (dots) to stitch strings, while print does not. Using echo is a little more efficient (no return value) if you simply show it.

5.var_dump
Print the information about the variable,

This function displays structure information about one or more expressions, including the type and value of the expression. The array recursively expands the value and displays its structure by indentation.
String variables
$a = "123";
Var_dump ($a);
Run Result: string (3) "123"
Print out the variables: type, length, value;


Number of numeric types
$b = 456;
Var_dump ($b);
Operation Result: Int (456)
Print out the variables: type, value values;


Array
$c = Array (' php start ', ' phpqidian.com ');
Var_dump ($c);
Run Result: Array (2) {
[0]=>
String (9) "Php enchanters Wind"
[1]=>
String ("phpqidian.com")
}
Print out the array's: type, number of elements, and the information corresponding to the element;


Boolean type variable
$d = true;
Var_dump ($d);
Run Result: bool (TRUE)

Common PHP variable output: Echo, Prinf, sprintf, Var_dump

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.