Php output statement

Source: Internet
Author: User
I. echoecho () is actually not a function, but a php statement, so you do not need to use parentheses for it. However, if you want to pass more than one parameter to echo (), a parsing error occurs when brackets are used. Besides, echo returns void and does not return the value. Therefore, you cannot use it... SyntaxHighlighter. all ();

1. echo
Echo () is actually not a function, it is a php statement, so you do not need to use parentheses for it. However, if you want to pass more than one parameter to echo (), a parsing error occurs when brackets are used. Besides, echo returns void and does not return values. Therefore, it cannot be used to assign values.
Example:

$ A = echo ("xshell"); // error! Cannot be used for value assignment
Echo "xshell"; // xshell
Echo ("xshell"); // xshell
Echo ("xshell", "net"); // an error occurs. multiple parameters cannot be passed in parentheses.
Echo "xshell", "net", "is", "web"; // when no parentheses are needed, multiple values can be separated by commas to output xshell net is web
Echo "xshell is
Good
Web. "; // whether or not the line feed is displayed, the final display is a line of xshell is good web.
Echo "$ fistname net"; // if $ firstname = "xshell", xshell net is output.
Echo '$ firstname net'; // because single quotes are used, the value of $ firstname is not output, but $ firstname net is output.
?>

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:

$ A = print ("xshell"); // 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:

$ A = "xshell ";
$ C = print_r ($ );
Echo $ c; // The value of $ c is TRUE.
$ C = print_r ($ a, ture );
Echo $ c; // The value of $ c is a string xshell.
?>

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:

Printf ("My name is % s. "," Xshell "," net "); // My name is xshell net.
Printf ("My name is % 1 \ $ s % 1 \ $ s", "xshell", "net "); // 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", "xshell", "net"); // My name is net xshell
?>

5. sprintf functions
This function is used in the same way as printf. The only difference is that the function writes formatted strings to a variable instead of output.
Example:

Sprintf ("My name is % 1 \ $ s % 1 \ $ s", "xshell", "net"); // you will find nothing to output.
$ Out = sprintf ("My name is % 1 \ $ s % 2 \ $ s", "xshell", "net ");
Echo $ out; // output My name is xshell net
?>

VI. var_dump function
Function: outputs the content, type, and length of the variable, type, or string. It is often used for debugging.

$ A = 100;
Var_dump ($ a); // int (100)
$ A = 100.356;
Var_dump ($ a); // float (100.356)
?>

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.