Saturday, October 7, 2017

Difference among these echo , print ,print_r and var_dump in PHP


The echo and print are more or  less the same .
They are both language constructs that display string (or string variable that may be contain any value or string message)
The difference is that are..
 print has a return value of 1 so it can be used in expressions whereas echo has a void return type;
 echo can take multiple parameters ,although such usage is rare;

print_r prints a variable in a more human readable form.
var_dump prints out a detailed dump of a variable, including its typea and type of sub items .


echo-

Outputs one or more strings separated by commas
No return value

e.g. echo "Hello", "PHP"

print-

Outputs only a single string
Returns 1, so it can be used in an expression

e.g. print "Welcome to PHP World"

or, if ($expr && print "foo")

print_r()-

Outputs a human-readable representation of any one value
Accepts not just strings but other types including arrays and objects, formatting them to be readable
Useful when debugging
May return its output as a return value (instead of echoing) if the second optional argument is given

var_dump()-

Outputs a human-readable representation of one or more values separated by commas
Accepts not just strings but other types including arrays and objects, formatting them to be readable
Uses a different output format to print_r(), for example it also prints the type of values
Useful when debugging
No return value

var_export()-

Outputs a human-readable and PHP-executable representation of any one value
Accepts not just strings but other types including arrays and objects, formatting them to be readable
Uses a different output format to both print_r() and var_dump() - resulting output is valid PHP code!
Useful when debugging
May return its output as a return value (instead of echoing) if the second optional argument is given

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...