From my understanding of the functions echo(), print(), and print_r(), echo can input string variable constants. Print is similar to echo, but print_r can print arrays while the first two cannot. Below Let me introduce the usage and differences between the three of them in detail.
Echo is a PHP statement, print and print_r are functions. Statements have no return value, but functions can have return values (even if they are useless)
print() can only print out the value of simple type variables (such as int, string)
print_r() can print out the value of complex type variables (such as arrays, objects)
echo outputs one or more strings
echo — Output one or more strings
Descrīption
void echo ( string arg1 [, string ...] ) //The return value is empty
The code is as follows |
Copy code |
echo "Hello","Friend";
代码如下 |
复制代码 |
echo "你好","朋友";
|
|
print – output a string
Descrīption
int print (string arg)//The return value is an integer
The code is as follows |
Copy code |
print "Hello friend";
|
You can perform the following operations
代码如下 |
复制代码 |
$name=print "nihao n";
$str = 'test print value is'.$name;
eval("$print="$str";");
echo $print;
|
The code is as follows |
Copy code |
$name=print "nihao n";
$str = 'test print value is'.$name;
eval("$print="$str";");
echo $print;
|
print_r — Print human-readable information about a variable.
bool print_r (mixed expression [, bool return] ) //The return value is of Boolean type, and the parameters are of mix type, which can be strings, integers, arrays, and object classes. print_r() displays easy-to-understand information about a variable. If a string, integer, or float is given, the variable value itself is printed. If an array is given, the keys and elements will be displayed in a certain format. object is similar to an array.
print_r() will move the array pointer to the end.
You can
print_r(str);
print_r(int);
print_r(array);
print_r(obj);
Let’s give examples one by one
Four ways to output strings. echo
print()
printf()
print_r()
echo
代码如下 |
复制代码 |
if (!print("Hello, World")){
die("you are not listening to me");
}
|
Multiple values can be output 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.
The syntax is correct: echo "Hello", "World";
Syntax error: echo ("Hello", "World");
print()
The function print() prints a value (its argument) and returns true if the string is successfully displayed, false otherwise. For example,
The code is as follows |
Copy code |
if (!print("Hello, World")){
die("you are not listening to me");
}
|
printf()
printf() is derived 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;
Arguments arg1, arg2, ++, etc. are inserted into the main string at the percent sign (%) 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
The code is as follows
代码如下 |
复制代码 |
$str = "Hello";
$number = 123;
printf("%s world. Day number %u",$str,$number);
?>
#Results======
Hello world. Day number 123
|
|
Copy code
|
$str = "Hello";
代码如下 |
复制代码 |
$number = 123;
printf("With 2 decimals: %1$.2fbr />With no decimals: %1$u",$number);
?>
#Result
With 2 decimals: 123.00
With no decimals: 123
|
$number = 123;
printf("%s world. Day number %u",$str,$number);
代码如下 |
复制代码 |
$a = array('name' => 'Fred', 'age' => '15', 'wife' => 'Wilma');
print_r($a);
Output: Array
{
[name] => Fred
[age] => 15
[wife] => Wilma
}
|
?>
#Results======
代码如下 |
复制代码 |
class P {
var $name = 'nat';
// ...
}
$p = new P;
print_r($p);
Output: Object
{
[name] => nat
}
|
Hello world. Day number 123
|
If there are more % symbols than arg arguments, you must use placeholders. The placeholder is inserted after the % sign and consists of a number and "$". See example 3.
Example: ?php
The code is as follows
Copy code
|
$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()
The code is as follows
|
Copy code
|
$a = array('name' => 'Fred', 'age' => '15', 'wife' => 'Wilma');
print_r($a);
Output: Array
{
[name] => Fred
[age] => 15
[wife] => Wilma
}
The same goes for objects. For example,
The code is as follows
|
Copy code
|
class P {
var $name = 'nat';
// ...
}
$p = new P;
print_r($p);
Output: Object
{
[name] => nat
}
But the results of print_r() outputting Boolean values and NULL are meaningless, because they all print "n". Therefore, using the var_dump() function is more suitable for debugging
http://www.bkjia.com/PHPjc/631244.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631244.htmlTechArticleFrom my understanding of the echo(), print(), print_r() functions, echo can input a string Variable constants, print and echo are similar, but print_r can print arrays but the first two cannot. Below I...
|
|
|
|