Any way to break if statement in PHP?

前端 未结 20 1926
無奈伤痛
無奈伤痛 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:12

    Don't worry about other users comments, I can understand you, SOMETIMES when developing this "fancy" things are required. If we can break an if, a lot of nested ifs won't be necessary, making the code much more clean and aesthetic.

    This sample code illustrate that CERTAINS SITUATIONS where breaked if can be much more suitable than a lot of ugly nested ifs... if you haven't faced that certain situation does not mean it doesn't exists.

    Ugly code

    if(process_x()) {
    
        /* do a lot of other things */
    
        if(process_y()) {
    
             /* do a lot of other things */
    
             if(process_z()) {
    
                  /* do a lot of other things */
                  /* SUCCESS */
    
             }
             else {
    
                  clean_all_processes();
    
             }
    
        }
        else {
    
             clean_all_processes();
    
        }
    
    }
    else {
    
        clean_all_processes();
    
    }
    

    Good looking code

    do {
    
      if( !process_x() )
        { clean_all_processes();  break; }
    
      /* do a lot of other things */
    
      if( !process_y() )
        { clean_all_processes();  break; }
    
      /* do a lot of other things */
    
      if( !process_z() )
        { clean_all_processes();  break; }
    
      /* do a lot of other things */
      /* SUCCESS */
    
    } while (0);
    

    As @NiematojakTomasz says, the use of goto is an alternative, the bad thing about this is you always need to define the label (point target).

    0 讨论(0)
  • 2020-12-02 05:12
    $a="test";
    if("test"!=$a)
    {
    echo "yes";                   
    }
     else
     {
      echo "finish";
    }
    
    0 讨论(0)
  • 2020-12-02 05:13

    goto:

    The goto operator can be used to jump to another section in the program. The target point is specified by a label followed by a colon, and the instruction is given as goto followed by the desired target label. This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure. You may jump out of these, and a common use is to use a goto in place of a multi-level break...

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

    You could use a do-while(false):

        <?php
        do if ($foo)
        {
          // Do something first...
    
          // Shall we continue with this block, or exit now?
          if ($abort_if_block) break;
    
          // Continue doing something...
    
        } while (false);
        ?>
    

    as described in http://php.net/manual/en/control-structures.if.php#90073

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

    Because you can break out of a do/while loop, let us "do" one round. With a while(false) at the end, the condition is never true and will not repeat, again.

    do
    {
        $subjectText = trim(filter_input(INPUT_POST, 'subject'));
        if(!$subjectText)
        {
            $smallInfo = 'Please give a subject.';
            break;
        }
    
        $messageText = trim(filter_input(INPUT_POST, 'message'));
        if(!$messageText)
        {
            $smallInfo = 'Please supply a message.';
            break;
        }
    } while(false);
    
    0 讨论(0)
  • 2020-12-02 05:17

    No, there is no way to "break" an if block like you would inside loops.:(
    So turn your test into a switch !

    I wonder why nobody encouraged you to use switch statement since (even if you haven't to many test cases)
    Do you think it's too verbose?

    I would definitely go for it here

      switch($a){
        case 'test':
            # do stuff here ...
            if(/* Reason why you may break */){
               break; # this will prevent executing "echo 'yes';" statement
            }
            echo 'yes';  # ...           
            break; # As one may already know, we might always have to break at the end of case to prevent executing following cases instructions.
        # default:
            # something else here  ..
            # break;
      }
    

    To me Exceptions are meant to raise errors and not really to control execution flaw.
    If the break behaviour you are trying to set is not about unexpected error(s), Exception handling is not the right solution here :/.

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