PHP learning output string (echo, print, printf, print_r and var_dump) _ php

Source: Internet
Author: User
In PHP, there are four methods to output strings. The echo structure can output multiple values at a time; print () can only output one value; printf () can format the output; print_r () can output arrays, which is very beneficial for debugging.

In PHP, there are four methods to output strings. The echo structure can output multiple values at a time; print () can only output one value; printf () can format the output; print_r () can output arrays, which is very beneficial for debugging.

The following is a one-to-one introduction.
1. echo
Echo is a keyword of PHP, and it does not return a value. In terms of writing, it can omit parentheses. The following code:
The Code is as follows:
Echo 'test string ';
Echo ('test string ');

2. print
Print is also a keyword in PHP. It has a returned value. Generally, true is returned, and false is returned. In terms of writing, it is the same as echo, and parentheses can be omitted. The following code:
The Code is as follows:
Print 'test string ';
Print ('test string ');

3. printf
Printf can format and output a string like printf in C language. The format is similar to that in C, both of which start with %. Its specifiers are defined as follows.
The B parameter is an integer and its binary value is displayed.
The c parameter is an integer that displays the corresponding ASCII characters
The value d is an integer in decimal format.
The f parameter is double-precision and displayed as a floating point number.
The e parameter is double-precision and displayed as a scientific counting type.
The g parameter is double-precision and displayed as a floating point or scientific counter.
The o parameter is an integer and Its octal value is displayed.
The s parameter is a string and displayed as a string.
The u parameter is an unsigned integer and is displayed in decimal format.
The x/X parameter is an integer and is displayed in hexadecimal format (case sensitive)
% Output % should indicate:
F and e are the six digits after the decimal point by default. If g is more than six digits (plus the decimal point), it is rounded to the nearest integer. If the value is smaller than 1000000, It is output directly, if the value is greater than 1000000, it is displayed as a scientific counting type. F is incorrect when the output value is greater than 1.2e23.
In addition to %, you can specify the total number of digits of the output (decimal point and E are regarded as one digit), and you can specify 0 or space as the complement character, you can also specify whether the complement position is left or right.
F, e can specify the digits after the decimal point.
For example, % 5d indicates that the total number of output digits is 5, and less than left fill space; % 05d indicates that the total number of output digits is 5, and less than left fill 0; % 05.1f indicates that the total number of output digits is 5, and less than left fill 0, 1 digit after the decimal point; %-05.1f indicates that the total number of digits in the output is 5. If not, add 0 to the right and 1 to the decimal point;
Sample Code:
The Code is as follows:
Printf ("% 7.2f", 1.2); // "1.20"
Printf ("%-07.2f", 1.2); // "1.20000"

4. sprintf
Sprintf and format conversion are the same as printf. The difference between them is that printf outputs directly, while sprintf returns a formatted string.
5. print_r and var_dump
Both print_r and var_dump can output arrays and objects, but print_r is not obvious about Boolean output. var_dump outputs are more detailed and are generally used for debugging.
The following code:
The Code is as follows:
$ V = new test ();
Print_r ($ v );
Var_dump ($ v );
Class test {
Public $ num = 1;
Public $ str = "222 ";
Public $ bln = true;

Result:
The Code is as follows:
Test Object
(
[Num] => 1
[Str] = & gt; 222
[Bool] => 1
)
Object (test) #1 (3 ){
["Num"] =>
Int (1)
["Str"] =>
String (3) 222"
["Bool"] =>
Bool (true)
}

References:
PHP programming, chapter 4, Chapter 4 string, output string

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.