Home  >  Article  >  Backend Development  >  The difference between PHP echo, print_r(expression), var_dump(expression), print_rvar_dump_PHP tutorial

The difference between PHP echo, print_r(expression), var_dump(expression), print_rvar_dump_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:18:00971browse

PHP echo, print_r(expression), var_dump(expression) difference, print_rvar_dump

All three are PHP statements with output function, but print_r(expression), var_dump(expression) is a function, echo is just a language structure, not a function, so it cannot be used as part of the expression.
For php’s 8 data types,

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/886484.htmlTechArticlePHP echo, print_r (expression), var_dump (expression) difference, print_rvar_dump all three are php with output function statement, but print_r(expression), var_dump(expression) are functions, and echo is just...
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