Home >Backend Development >PHP Problem >What is the difference between exit and return in php

What is the difference between exit and return in php

王林
王林Original
2020-08-12 13:27:023499browse

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';

(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';

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!

Statement:
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