PHP Print output function usage detailed

Source: Internet
Author: User
Tags mixed php print sprintf

The function of the printing output commonly used in PHP is explained Step-by-step

Echo

The Echo function can output multiple strings at the same time, with multiple arguments, but does not require parentheses and no return value. However, if there is no problem with parentheses, the function will need the parentheses:


echo ' Standard technology blog <br> ';
Echo (' echo can also be parenthesized <br> ');

Believe that the Echo function is the most used print function!

Print
The print function can output only one string at a time, with only one argument, with parentheses and a return value. Returns to Flase when its execution fails.


Print (' www.111cn.net<br> ');

The print function is also pretty much used to print information, but it's not as convenient as ECHO, but it's useful!

Printf
The printf function has two parameters, the first parameter is the specified output format, and the second parameter is the variable to output. The output format is:

%s: by string;
%d: by integral type;
%b: According to the binary;
%x: Press 16;
%o: Press octal;
$f: by floating-point type

/*
$var = 10;
printf (' Integral type:%d<br> ', $var);
printf (' Floating-point type:%.2f<br> ', $var); Keep two decimal digits
printf (' String:%s<br> ', $var);
printf (' Binary:%b<br> ', $var);
printf (' Octal:%o<br> ', $var);
printf (' Hexadecimal:%x<br> ', $var);

Print results
/*
Integral type: 10
Floating-point type: 10.00
String: 10
Binary system: 1010
Octal System: 12
Hex: A
*/

sprintf

Instead of directly outputting the variable value, sprintf reads the value directly to the specified variable:

$ret = sprintf ('%.2f ', $var);
echo "Result: {$ret}<br>";

This function is useful for formatting variable output, and it is used a lot!

Print_r

Print_r This function is used to output an array, with one or two. If the parameter two is set to Yes, the expression information is not printed, but it is returned directly:


Mixed Print_r (mixed $expression [, bool $return = false])

$arr = Array (' name ' => ' Technical blog ', ' site ' => ' www.111cn.net ');
Print_r ($arr);
Echo ' <br> ';

Parameter two is set to true and is not printed, but is returned directly
$arr 1 = Print_r ($arr, true);
echo "{$arr 1}<br>";

Var_dump

Var_dump This function is used most in the debugging process, the content of the output variables, type, string content, often used in development debugging use:


When a string is printed, which has an object, it is printed:
String (Var_dump:array)
Var_dump (' Var_dump: '. $arr. ' <br> ');

When only the object itself is printed as follows:
"Array (2) {[" Name "]=> string (21)" Technical Blog for Diego "[" Site "]=> string ()" Www.111cn.net "}
Var_dump ($arr);

Die

The die function is also widely used, during debugging, often interrupts the following execution, it will first output the content, and then exit the program or do not output content:


if (!isset ($type)) {
Die (' I am die!<br> ');
}

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.