返回早期概念如何在PHP中有用

I have gone through the best practises in the following link https://pear.php.net/manual/en/standards.bestpractices.php. I am still not quite clear about the return early concept in PHP . Is it used to reduce the no of else conditions in a PHP function ? When should we ideally use this concept and why is it useful?

Short example why it can be usefull

//some demo function
function connect_to_ftp($host, $user, $pass){
    //some check if the host is a valid host, just for demonstration
    if (not_a_valid_host($host)) return false;
    //if the host is valid, continue executing, no else required
    make_ftp_connection($host, $user, $pass);
    return true;
}

This is a very crude and oversimplified test but on PHP 7.0.2 test_func1() is 33% faster than test_func2():

<?php
function test_func1()
{
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){return;}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
}

function test_func2()
{
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    if(1===1){}
    return;
}

$iterations = 1000000;

$start = microtime(true);
for($i=0; $i<$iterations; ++$i)
{
    test_func1();
}
echo (microtime(true)-$start)."

";

$start = microtime(true);
for($i=0; $i<$iterations; ++$i)
{
    test_func2();
}
echo (microtime(true)-$start);

Try it yourself at http://sandbox.onlinephpfunctions.com/

Like I said, this is oversimplified. Imagine that instead the function makes multiple database calls to compare certain values. If the comparison of the first value renders the calls to subsequent comparisons useless then there is no reason to continue to those comparisons.

It's all about CLEAN CODE and readability

function bad($x,$y,$z){
 if($x){
   if($y){
       if($z){
           /*code work*/
       } else {
           return null;
       }
   } else {
       return null;
   }
  } else {
    return null;
  }
}

function better(){
   if(!$x){
      return null;
   } 
   if(!$y){
       return null;
   }
   if(!$z){
       return null;
   }
   /*code work*/
}

function bestversioninthiscase(){
  if(!$x || !$y || !$z){
      return null;
  } 
  /*code work*/
}

..not about about which part of the code gets executed.

Which is more readable here:

if($x){
  #code
}

or

if($x)
{
  #code
}

Have fun discussing this topic :)