Search code examples
phpif-statementreturnoperators

What does the return do at the end of the expression of a PHP condition?


I am analyzing a PHP code but I cannot understand the logic of a condition with a return at the end.

Route::domain('{name}.{domain}.{tls}')->group(function () {
    $settings = App::make(\Common\Settings\Settings::class);
    $appUrl = config('app.url');
    $currentUrl = \Request::url();
    if ($appUrl === $currentUrl || !$settings->get('builder.enable_subdomains')) return; //<- Here
    Route::get('{page?}', 'UserSiteController@show')->name('user-site-subdomain');
});

I need to understand what are the circumstances that line 6 will be executed but apparently the return changes everything.

Any clarification or some more illustrative example will be welcome.


Solution

  • It immediately ends this function to prevent

    Route::get('{page?}', 'UserSiteController@show')->name('user-site-subdomain');

    from being executed if conditions in

    if ($appUrl === $currentUrl || !$settings->get('builder.enable_subdomains'))

    are met ($appUrl === $currentUrl or !$settings->get('builder.enable_subdomains') equals true). Just like you would end a function normally with for e.g. return 4;, but you do not return any value.