What's the Difference Between Echo, Print, and Print_R in PHP

Difference between echo and print_r in php?

The difference is that print_r recursively prints an array (or object, but it doesn't look as nice), showing all keys and values. echo does not, and is intended for scalar values.

What is the difference between print_r and echo when providing results to an AJAX call?

The most common built-in output functions in PHP are: echo, print, printf, print_r, var_dump, and var_export. They are all designed to output data, albeit, in slightly different ways. Which is why there are specific use cases associated with each function.

  • echo: The most notorious. It's what you should be using to output data to an AJAX caller. It's simple and effective. It's actually a language construct, so it doesn't require you to use brackets. You just write echo 'string'; or echo $something;, etc.

    It also supports multiple comma-delimited values, making it handy in a variety of use cases; e.g., echo 'string1', 'string2', 'string3';. Note that echo returns null (nothing).


  • print: Also a language construct, it's nearly the same as echo, with the difference being that it accepts only one argument and always returns the same thing: 1. It's OK to use print instead of echo if you like, but many popular PHP projects have code standards that ask you not to do so. So print has become, more or less, an unused (or rarely used) function in PHP.

  • printf: This function and its brother sprintf() are designed to output formatted strings. It's slightly more complex, but does have specific use cases. For example, these are often used to output an integer as a binary number or octal number, for translation, among many other options that are well documented at PHP.net — that's a separate topic.

  • print_r, var_dump, and var_export: These are most commonly used when debugging code or testing code in one way or another. You will rarely find a use case for them in production code. That said, these allow you to pass in complex data types, such as an entire array, and print_r recursively, to dump the entire array for analysis. var_dump does pretty much the same thing, but uses a slightly different approach that can sometimes be useful. var_export has the advantage of outputting a parseable string.

    print_r and var_export both support a parameter that can 'return' the value it would normally output, which can be a handy feature. Something that var_dump is not capable of.

echo and (var_dump or print_r) showing completely different things on laravel object

In Laravel 5 you should use dd, e.g.:

dd($links);

or in view:

{{ dd($links) }}

dd will prevent those recursive warnings.

php var_dump() vs print_r()

The var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.

The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.

Example:

$obj = (object) array('qualitypoint', 'technologies', 'India');

var_dump($obj) will display below output in the screen.

object(stdClass)#1 (3) {
[0]=> string(12) "qualitypoint"
[1]=> string(12) "technologies"
[2]=> string(5) "India"
}

And, print_r($obj) will display below output in the screen.

stdClass Object ( 
[0] => qualitypoint
[1] => technologies
[2] => India
)

More Info

  • var_dump
  • print_r

Why do print and echo behave differently in a for loop

Expression. print() behaves like a function in that you can do: $ret
= print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An
example from the PHP Manual:

$b ? print "true" : print "false";

Some part of my answer are part of below answer. I think this is the answer of your question. Most important part is print() behaves like a function

see this answer: https://stackoverflow.com/a/234255/1848929

What about echo:

Note: Because this is a language construct and not a function, it
cannot be called using variable functions.

see notes part on this page: http://us2.php.net/manual/en/function.echo.php



Related Topics



Leave a reply



Submit