Any way to break if statement in PHP?

前端 未结 20 1929
無奈伤痛
無奈伤痛 2020-12-02 05:05

Is there any command in PHP to stop executing the current or parent if statement, same as break or break(1) for switch/

相关标签:
20条回答
  • 2020-12-02 05:18

    i have a simple solution without lot of changes. the initial statement is

    I want to break the if statement above and stop executing echo "yes"; or such codes which are no longer necessary to be executed, there may be or may not be an additional condition, is there way to do this?

    so, it seem simple. try code like this.

    $a="test";
    if("test"==$a)
    {
      if (1==0){
          echo "yes"; // this line while never be executed. 
          // and can be reexecuted simply by changing if (1==0) to if (1==1) 
      }
    }
    echo "finish";
    

    if you want to try without this code, it's simple. and you can back when you want. another solution is comment blocks. or simply thinking and try in another separated code and copy paste only the result in your final code. and if a code is no longer nescessary, in your case, the result can be

    $a="test";
    echo "finish";
    

    with this code, the original statement is completely respected.. :) and more readable!

    0 讨论(0)
  • 2020-12-02 05:19

    What about using ternary operator?

    <?php
     // Example usage for: Ternary Operator
     $action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
    ?>
    

    Which is identical to this if/else statement:

    <?php
     if (empty($_POST['action'])) {
       $action = 'default';
     } else {
       $action = $_POST['action'];
     }
    ?>
    
    0 讨论(0)
  • 2020-12-02 05:25
    $arr=array('test','go for it');
    $a='test';
    foreach($arr as $val){
      $output = 'test';
      if($val === $a) $output = "";
      echo $output;
    }
    echo "finish";
    

    combining your statements, i think this would give you your wished result. clean and simple, without having too much statements.

    for the ugly and good looking code, my recomandation would be:

    function myfunction(){
      if( !process_x() || !process_y() || !process_z()) {
        clean_all_processes();  
        return; 
      }
    /*do all the stuff you need to do*/
    }
    

    somewhere in your normal code

    myfunction();
    
    0 讨论(0)
  • 2020-12-02 05:25

    I'm late to the party but I wanted to contribute. I'm surprised that nobody suggested exit(). It's good for testing. I use it all the time and works like charm.

    $a ='';
    $b ='';
    if($a == $b){
    echo 'Clark Kent is Superman';
    exit();
    echo 'Clark Kent was never Superman';
    }
    

    The code will stop at exit() and everything after will not run.

    Result

    Clark Kent is Superman
    

    It works with foreach() and while() as well. It works anywhere you place it really.

    foreach($arr as $val)
    {
      exit();
      echo "test";
    }
    
    echo "finish";
    

    Result

    nothing gets printed here.
    

    Use it with a forloop()

    for ($x = 2; $x < 12; $x++) {
        echo "Gru has $x minions <br>";
        if($x == 4){
        exit();
        }
    }
    

    Result

    Gru has 2 minions
    Gru has 3 minions
    Gru has 4 minions
    

    In a normal case scenario

    $a ='Make hot chocolate great again!';
    echo $a;
    exit();
    $b = 'I eat chocolate and make Charlie at the Factory pay for it.';
    

    Result

    Make hot chocolate great again!
    
    0 讨论(0)
  • 2020-12-02 05:26

    Encapsulate your code in a function. You can stop executing a function with return at any time.

    0 讨论(0)
  • 2020-12-02 05:27

    proper way to do this :

    try{
        if( !process_x() ){
            throw new Exception('process_x failed');
        }
    
        /* do a lot of other things */
    
        if( !process_y() ){
            throw new Exception('process_y failed');
        }
    
        /* do a lot of other things */
    
        if( !process_z() ){
            throw new Exception('process_z failed');
        }
    
        /* do a lot of other things */
        /* SUCCESS */
    }catch(Exception $ex){
        clean_all_processes();
    }
    

    After reading some of the comments, I realized that exception handling doesn't always makes sense for normal flow control. For normal control flow it is better to use "If else":

    try{
      if( process_x() && process_y() && process_z() ) {
        // all processes successful
        // do something
      } else {
        //one of the processes failed
        clean_all_processes();
      }
    }catch(Exception ex){
      // one of the processes raised an exception
      clean_all_processes();
    }
    

    You can also save the process return values in variables and then check in the failure/exception blocks which process has failed.

    0 讨论(0)
提交回复
热议问题