php return語句另類用法不止是在函數(shù)中

字號:


    分享下php return語句的另一個作用,在bbpress的代碼中看到的一個奇葩使用方法。
    一直以為,return只能出現(xiàn)在函數(shù)中,直到看了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 還能出現(xiàn)在函數(shù)之外?這在c語言是無法想象的。
    查了一下 php 手冊:如果在一個函數(shù)中調(diào)用 return 語句,將立即結(jié)束此函數(shù)的執(zhí)行并將它的參數(shù)作為函數(shù)的值返回。如果在全局范圍中調(diào)用,則當(dāng)前腳本文件中止運行。