Home  >  Article  >  Backend Development  >  Analysis of the differences between echo, print_r and var_dump in PHP, print_rvar_dump_PHP tutorial

Analysis of the differences between echo, print_r and var_dump in PHP, print_rvar_dump_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:17:59845browse

Analysis of the difference between echo, print_r and var_dump in PHP, print_rvar_dump

This article provides a more detailed analysis of the differences between echo, print_r and var_dump in PHP. Share it with everyone for your reference. The specific analysis is as follows:

All three are PHP statements with output functions, but print_r(expression) and var_dump(expression) are functions, and echo is just a language structure, not a function, so it cannot be used as part of the expression.

For operating the 8 data types of php:

echo is used to output numerical variables or strings. But when using echo to output reference variables, such as arrays, only the name of the array is output; when outputting an object, the server prompts error, and the error is displayed Echo can only be used to output strings.

The function of print_r(expression) is to output an array. In fact, the type of parameter expression can be numeric variables and reference variables .

③ The output result of the var_dump(expression) function is . The parameter expression represents various variable types, and its function is to output the detailed information of a variable.

I hope this article will be helpful to everyone’s PHP programming design.

What data types can echo, print_r and var_dump in PHP output? How are they different?

echo 'Output a string';//Only strings and numbers can be output

print_r — Print easy-to-understand information about variables, generally using the output array structure

var_dump — Print relevant information about variables, generally used for array and object printing

You will know it after using it yourself

What are the differences between echo(), print(), and print_r() in PHP?

Four ways to output strings. echo

print()

printf()

print_r()
echo
can output multiple values ​​at one time, separated by commas . echo is a language construct, not a real function, so it cannot be used as part of an expression.

Correct syntax: echo "Hello", "World";
Wrong syntax: echo ("Hello", "World");
print()
The function print() prints a Value (its argument), returns true if the string was successfully displayed, false otherwise. For example, if (!print("Hello, World")){

die("you are not listening to me");

}
printf()
printf () originates from printf() in C language. This function outputs a formatted string.
Syntax: printf(format,arg1,arg2,arg++)
format specifies the string and how to format the variables in it;
arg1, arg2, ++ and other parameters will be inserted into the main string Semicolon (%) symbol. This function is executed step by step. At the first % sign, arg1 is inserted, at the second % sign, arg2, and so on.
Example: ?php

$str = "Hello";

$number = 123;

printf("%s world. Day number %u", $str,$number);

?>
#Results======
Hello world. Day number 123

If there are more % symbols than arg parameters, then you must use placeholders. Placeholders are inserted after the % sign and consist of numbers and "\$". See example 3.
Example: ?php

$number = 123;

printf("With 2 decimals: %1\$.2fbr />With no decimals: %1\$u" ,$number);

?>
#Result
With 2 decimals: 123.00
With no decimals: 123

print_r() and var_dump()
print_r() can simply print out strings and numbers, while arrays are displayed as a bracketed list of keys and values, starting with Array. For example, $a = array('name' => 'Fred', 'age' => '15', 'wife' => 'Wilma');

print_r($a);
Output: Array

{

[name] => Fred

[age] => 15

[wife] => ; The same goes for Wilma

}
objects. For example, class P {

var $name = 'nat';

// ...

}

$p = new P;

print_r($p);
Output: Object

{

[name] => nat

}
but print_r( ) The results of outputting Boolean values ​​and NULL are meaningless, because they all print "...the rest of the full text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/887349.htmlTechArticleAnalysis of the differences between echo, print_r and var_dump in PHP, print_rvar_dump This article provides a more detailed analysis of echo, print_r and var_dump in PHP the difference. Share it with everyone for your reference. The specific analysis is as follows:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn