30 December 2023

In PHP, you can use the return statement to exit a function and return a value if needed. The return statement not only returns a value but also exits the function immediately.

Source code viewer
  1. function myFunction() {
  2. // Some code here
  3.  
  4. if ($someCondition) {
  5. // Break out of the function
  6. return $someValue;
  7. }
  8.  
  9. // More code here
  10.  
  11. // If the condition is not met, the function continues here
  12. }
  13.  
  14. // Call the function
  15. $result = myFunction();
Programming Language: PHP