Home > Article > Backend Development > Another function of PHP return statement, return statement_PHP tutorial
I always thought that return can only appear in functions, until I looked at the bbPress code:
<?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' ); } return; }
Can return still appear outside a function? This is unimaginable in C language.
Check the PHP manual: If you call the return statement in a function, it will immediately end the execution of this function and return its parameters as the value of the function. If called in the global scope, the current script file aborts running.
Alas, I am too deeply poisoned by the C language.
return is actually not difficult, it means return;
If return is executed, the content after the return statement will not be executed;
return can be a function return value , or you can return a null value, it depends on your specific usage, for example:
function test($a){
if($a>10){
return "a> 10";
}else{
return "a<10";
}
$b=45;
$c=$b-$a;
echo $c;
}
In this example, when you call this function and give any number, it will return a string, and the code: $b=45;
$c =$b-$a;
echo $c;
will never be executed
return means return.
echo is output. You can also print
return is not output,