分享下php return語句的另一個作用,在bbpress的代碼中看到的一個奇葩使用方法。
一直以為,return只能出現在函數中,直到看了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' );
} //
return;
}
難道 return 還能出現在函數之外?這在c語言是無法想象的。
查了一下 php 手冊:如果在一個函數中調用 return 語句,將立即結束此函數的執(zhí)行并將它的參數作為函數的值返回。如果在全局范圍中調用,則當前腳本文件中止運行。
更多信息請查看IT技術專欄