It is clear that the functions?
echo
print
print_r
var_dump
Yvar_export
they are used almost interchangeably to print content to the screen.
The objective of this question is to have a good answer that serves as a reference to understand the possible differences between each of them... if there are differences.
I liked your question, I found a related one on the English site, you can take a look at it here , I'll take the time to translate part of the highest voted answer in the above mentioned question, let's start:
threw out
Example:
print
Examples:
print_r()
true
as its second argumentExample: If we have the following arrangement:
Our
print_r($a);
will return something like this:var_dump()
print_r()
, for example, it also shows the type of the valueExample: If we have the following arrangement:
Our
var_dump($a);
will return something like this: (note that it shows us the value type of each element in our array)var_export()
var_dump()
andprint_r()
, the output is valid PHP codetrue
as its second argumentExample, if we do the following
We will obtain as a result:
GRADES
echo
is much faster thanprint
echo
andprint
they are language constructorsprint_r()
andvar_dump()
they are functionsecho
andprint
can receive their arguments via parentheses as a functionecho("Hello", "world");
orprint("Hello World");
The first big difference is that
print_r
andvar_dump
are real functions whileecho
andprint
are not really functions but language constructors.echo vs print
Both are language constructs that display text strings with somewhat subtle differences. This is the shape and type of each:
From the above definition we can deduce that:
print prints a string, echo can print more than one separated by commas:
print returns an int value that according to the documentation is always 1, so it can be used in expressions while echo is of type void, there is no return value and it cannot be used in expressions:
According to some sources ( https://www.w3schools.com/php/func_string_print.asp ) echo is slightly faster than print.
print_r vs var_dump
These two functions print the details of a variable, including its value, in a human-readable format. If it is an array or an object, they also print the details of each element. They are often used when debugging code, where var_dump is often more useful because of the more information it provides. The differences:
If the value of the variable is a string, var_dump prints the string enclosed in double quotes, print_r does not.
print_r does not print anything visible for false and empty strings.
var_dump provides information about the size and data type of the variable and, in the case of arrays and objects, of the elements that compose it. print_r gives no information about the size of the variable or the data type.
Example:
print_r can return the result instead of printing it if the second parameter is given as true:
var_export vs var_dump and print_r
Print valid php code. This is useful if you compute values that need to be constants in other scripts. Note that var_export does not handle cyclic/recursive array references, while var_dump and print_r do. Also, var_export prints the result by default, but enables the ability to return a defined string using the optional $return parameter.
This will print:
You can find more information at: ( https://cybmeta.com/php-differences-between-echo-print-print_r-y-var_dump )