The reason why echo is used when writing API output in PHP

little bottle
Release: 2023-04-06 12:20:01
forward
2352 people have browsed it

This article mainly talks about the reasons for using echo when writing API output in PHP. It has certain learning value. Interested friends can learn about it.

I rarely write php APIs. I have only recently begun to write interfaces. I have always used return in the framework, but retrun fails in the api. Why?

Answers given by netizens:

1.return is generally used to return functions or methods.
echo is the output (that is, display)
If you don't output, what is the use of the API.

2.return can only be used inside php
Externally, if you want to parse 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 the header identifier

<?php
$ret = array(
    &#39;name&#39; => 'fdipzone'
);
header('content-type:application/json;charset=utf8');
echo json_encode($ret);?>
Copy after login

Why do you need to use echo to return json data when writing an interface for an app?

1. When writing an interface for an app, you need to use echo json_encode() to return data , but PHP itself uses return json_encode();

rerurn is the value represented by the function or method in the language, which is stored in memory just like variables. Return is a pointer or reference to other code blocks.
When the server interacts with the app (client), they cannot read the same memory system and can only exchange information through text.
So we need to echo a piece of text (json) to the client.
The usual output stream method of PHP is echo. If you like return output, you can create a language to achieve it.

2. This is how I understand it, no matter what the program is, you always have to output it. So how do you output it? PHP scripts, that is, those marked with are all parsed by PHP, so if you want to output, you must use echo, die, exit, print_r,
use return for output No, return means return. I (the main program) call the method, and the method returns the result to me.

3. The framework itself will output the string returned by your function, and the output will eventually be echo. . It’s just that the framework basically encapsulates the response object; echo outputs only the output content and can only be obtained when making an http request
. However, there is no http request for internal calls in PHP

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

Because the front and back ends use different memory systems, so The corresponding data cannot be found based on the address, and echo print sprinf and other functions that can print data in the browser are all real data transmitted based on the http protocol, so it can be obtained and judged on 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 cannot be used with native php. This should be tp The inside of the frame has been processed!

Related tutorials: PHP video tutorial

The above is the detailed content of The reason why echo is used when writing API output in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template