In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "why echo is used when PHP writes API output", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Why use echo when PHP writes API output"!
Answer:
1. Return is generally used for the return of a function or method.
Echo is the output (i.e. display)
What does api do if you don't output.
Return can only be used inside php
Externally, if you want to parse it for others, you understand it as output, use echo.
3. Of course it's echo, if it's json, it's best to add the header identifier.
Why do I return json data with echo when writing interfaces to apps?
1. When writing interfaces to apps, echo json_encode() is used to return data, but PHP itself calls return json_encode();
A reurn is a value represented by a function or method in a language, stored in memory like a variable. Return is a pointer or reference to another block of code.
When a server interacts with an app (client), they cannot read the same memory system and can only exchange information through text.
So echo a paragraph of text (json) to the client.
Php's usual output stream is echo, and if you prefer return output, you can create your own language to implement it.
2. I understand this, no matter what program, you always have to output it, then how to output it? PHP scripts, that is, those marked up, all rely on PHP to parse, then you must use echo,die,exit,print_r to output,
Return is not output, return is the meaning of return, I (the main program) call the method, the method returns the result to me.
3. The framework itself will output the string returned by your function, and the output will end up as echo. Only the framework basically encapsulates the response object;echo is the output. Only the output content can be obtained when http request.
php calls without http requests
Return is a pointer to the returned data, i.e., the address where the data is stored, and the interface needs to return the actual data.
Because the front and rear end is using different memory systems, so can not find the corresponding data according to the address, and echo print sprinf and other functions that can print data in the browser are based on http protocol transmission of real data, so in the front end is can be obtained and judged.
It should be noted that in tp framework, the return keyword is indeed able to return data, that is, it can be used as an interface to return data keywords, but it is not possible to use native php, which should be handled within tp framework!
At this point, I believe that everyone has a deeper understanding of "why use echo when writing API output in PHP", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.