The reason why echo is used when PHP writes API output is explained in detail

  • 2021-12-05 05:55:25
  • OfStack

Write php API write very little, only recently started interface writing, in the framework 1 directly use return, but in api retrun is invalid, why?

The answer given by netizens:

1. return 1 is generally used for the return of a function or method.

echo is the output (that is, the display)

If you don't output, what is the effect of api?

2. return can only be used inside php

Externally, if you want to analyze it for others, you will understand it as output and use echo

3. Of course, it is echo. If it is json, it is best to add header logo


<?php

$ret = array(

  'name' => 'fdipzone'

);

header('content-type:application/json;charset=utf8');

echo json_encode($ret);?>

Why use echo to return json data when writing an interface to app?

1. When writing an interface to app, echo json_encode () is used to return data, but PHP itself is called with return json_encode ();

rerurn is the value represented by a function or method in the language, which is stored in memory like variable 1. return is a pointer or reference to other code blocks.

When the server interacts with app (client), they can't read the same memory system, and can only exchange information by text.

So give the echo1 segment text (json) to the client.

php's usual output stream is echo. If you like return output, you can create a language to realize it.

2. I understand it this way. No matter what program, you always have to output it. How can you output it? PHP script, which uses the < ?php ? > All the marked ones have to be parsed by PHP, so if you want to output, you must use echo, die, exit, print_r.

return is not output, return is the meaning of return, I (main program) tune the method, the method to return results to me.

3. The framework itself will output the string returned by your function, and the output will eventually be echo. . Only the framework basically encapsulates response objects; The output of echo can only be obtained when the output content is requested by http

The php internal call has no http request

return, as a function return value or end statement, is a pointer to the returned data, that is, the address where the data is stored, and the interface needs to return real data.

Because the front and back ends use different memory systems, the corresponding data cannot be found according to the address, and the functions that can print data in the browser, such as echo print sprinf, are all real data transmitted based on http protocol, so they can be obtained and judged at the front end.

It should be noted that in the tp framework, the return keyword can indeed return data, that is, it can be used as an interface to return data keywords, but it is impossible to use the native php, which should be handled within the tp framework!

The above is all about the reason why echo is used when PHP writes API output. Thank you for reading and supporting this site.


Related articles: