1

Although I have found some discussion about the general differences between echo and print_r() in PHP (for example, What's the difference between echo, print, and print_r in PHP?), all the tutorials and answers I've come across for AJAX calls say PHP should echo the results, and then die.

In an experiment returning a simple string, I didn't see any difference between the two when displayed using console.log.

Is there any significant difference in what is returned to the AJAX call when using echo vs print_r. Not that I see anything wrong with using echo, but I did get unexpected results when I used print_r earlier in the function for debugging.

tim.rohrer
  • 246
  • 3
  • 12
  • print and echo are more or less synonyms - they simply output a string whereas print_r will allow you to print out an array/object/other to give a visual clue when debugging more than anything – Professor Abronsius Nov 07 '17 at 22:29
  • 2
    If your AJAX is expecting json (common), using print_r will certainly mangle your output. – IncredibleHat Nov 07 '17 at 22:31
  • @Randall So, yes, there is a difference in what is returned? Is this because, as the linked answer states, echo can handle multiple strings while print_r is limited to one? – tim.rohrer Nov 07 '17 at 22:36

1 Answers1

4

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.

jaswrks
  • 1,255
  • 10
  • 13
  • I acknowledge I did not directly ask why echo is a better choice (assuming it is), and I appreciate that you alluded to it. Any more reasons why echo is what we should use? @Randall alluded to print_r garbling output, but if I understand echo, it is actually only useful for the simplest of returns? – tim.rohrer Nov 07 '17 at 23:21
  • 1
    Quite welcome. `echo` is useful for nearly all output that you'll do in PHP. If you're outputting something and you're _not_ using `echo`, you'll have a very good reason for doing so, and it will be a special case; e.g., you'll be using `printf()` or `echo sprintf()` for something special. In all other cases, you output with `echo` as a best practice. Or, you can think of it this way: the 'format' is generally being handled by your script. When it's time to output, you `echo`. The format has already been established elsewhere. – jaswrks Nov 07 '17 at 23:24
  • If the `echo` function were named `output`, it would probably be less confusing, because that's _all_ it does. Nothing more. It's just outputting something that has already been formatted. The same with `print`. – jaswrks Nov 07 '17 at 23:29
  • I wouldn't say `print_r` garbles output, I'd say that it outputs data for analysis, which can look like gobbly-goop to a user. However, it's in a specific format that's primarily intended for developer consumption. The same with `var_dump` and `var_export`. – jaswrks Nov 07 '17 at 23:30