Alternative usage of PHP return statement is not only in functions

  • 2021-07-18 07:40:33
  • OfStack

Share another function of PHP return statement and a wonderful use method seen in bbPress code.

1 Think that return can only appear in functions until you look at the code for bbPress:


<?php
require_once('./bb-load.php');

bb_repermalink(); // The magic happens here.

if ( $self ) {
if ( strpos($self, '.php') !== false ) {
require($self);
} else {
require( BB_PATH . 'profile-base.php' );
} // www.ofstack.com
return;
}

Can return still appear outside the function? This is unimaginable in C language.

Check the PHP manual under 1: If you call the return statement in 1 function, it will immediately end the execution of this function and return its parameters as the value of the function. If called in global scope, the current script file stops running.


Related articles: