What is the difference between exit and return in php

王林
Release: 2023-03-04 11:10:01
Original
3347 people have browsed it

The difference between exit and return in php is: exit means that the entire script stops running and does not output content, but exit() can output content; return means return value, jump out of the function, but does not stop the running of the script.

What is the difference between exit and return in php

Difference:

exit stops the program and does not output the content.

return is the return value, which jumps out of the function but does not stop the program.

(Recommended tutorial:php graphic tutorial)

Detailed description: Adding a () after

exit can also output content! return means return and also has the function of terminating the program.

php does not run the following code when it encounters return, but it is essentially different from termination.

Example:

function test(){ echo 'test1'; return; echo 'test2'; } echo 'test3';
Copy after login

(Video tutorial recommendation:php video tutorial)

Test1test3 is output here. The reason why test2 is not output is because in a When return is encountered in a function, it will return and the code within the function will no longer be executed, but the code outside the function will still be executed.

function test_1(){ echo 'test1'; exit; echo 'test2'; } echo 'test3';
Copy after login

Test1 is output here. When the script encounters exit; the entire script is no longer executed, whether outside the function or within the function.

The above is the detailed content of What is the difference between exit and return in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!