What are the differences between echo(), print(), print_r(), and return in PHP?
There are often many novices and friends who don’t use print() well who cannot answer this question and therefore fail the interview.
So what is the difference between them? Let’s take a look below
Answer:
echo is a PHP statement, print and print_r are functions, statements have no return value, and 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 array, object)
return statement exits from the current function and exits from that The function returns a value.
echo outputs one or more strings
echo — outputs one or more strings
Descrīption
void echo ( string arg1 [, string ...] ) / /The return value is empty
echo "Hello", "Friend";
print – Output a string
Descrīption
int print ( string arg )//The return value is an integer
print “Hello friend”;
You can perform the following operations
$name=print “nihao /n”;
$str = ‘test print value is $name.’;
eval(“/$print=/”$str /”;”);
echo $print;
print_r — Print easy-to-understand information about a variable.
bool print_r (mixed expression [, bool return] ) //The return value is Boolean, the parameter is of mix type, which can be string, integer, array, object class print_r() displays about a variable of easy-to-understand information. 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.
Note: print_r() will move the array pointer to the end