PHP Print output function Summary _php tips

Source: Internet
Author: User
Tags learn php mixed php print sprintf

This article has been to learn PHP all the functions used for output information, classification comparison to learn the same point and different points, master the necessary skills in daily development.

Development is bound to require debugging code, and PHP debugging code can not be like the compiler language of iOS such as Xcode through the development of software such as a single step, so the developer of PHP will usually print the output function of the relevant information to debug the code.

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.huangyibiao.com<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 = ten;
printf (' Integral type:%d<br> ', $var);
printf (' Floating-point type:%.2f<br> ', $var); retains two decimal digits
printf (' String:%s<br> ', $var);
printf (' Binary:%b<br> ', $var);
printf (' Octal:%o<br> ', $var);
printf (' Hexadecimal:%x<br> ', $var);
 
Print result/
*
integer: Ten
floating-point type: 10.00
string: binary
: 1010
octal:
16: 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.huangyibiao.com ');
Print_r ($arr);
Echo ' <br> ';
 
Parameter two is set to true without printing, but directly returns
$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)" Standard Brother's Technical blog "[" Site "]=> string (19)] Www.huangyibiao.com "}
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> ');
}

Summarize:

The above functions can print the value of the object, the value of the system function, and the contents of the array;
echo, print, and printf can print variable content, but cannot display array and system super variable array;
Print_r and Var_dump can not only print arrays, scalar variables, but also print the contents of objects;
Var_dump statements can not only print variables, array contents, but also display the contents of Boolean variables and resources (resource);
The Var_export function returns the structural information about the variable passed to the function, similar to the Var_dump () function, and the difference is that the returned content is valid PHP code.

The above is the summary of this article to the introduction of all the PHP print output function, I hope that the small partners can like

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.