简体   繁体   中英

PHP switch not working as expected

I have a PHP class called ResetPassword. Within this class is a switch which will return a different string containing the required HTML.

This is the code below (I have omitted a few of the cases for the sake of brevity, there is also a default case which returns NULL)

    public function get_display_block($displayblock) {
     switch ($displayblock) {
    case 'EnterNewPassword':
        $displayblock = '
                        <form action="resetpassword.php" method="post">
                        <fieldset class="login">
                        <legend>Enter your information in the form below to reset your password: </legend>
                        <div><label for="password1">Password :</label>
                        <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                        <div><label for="password2"><span class="required">*</span>Retype Password :</label>
                        <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                        <br />
                        </fieldset><br />
                        <br />
                        <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                        </form>
                        ';
            return $displayblock;
        break;

    case 'THIS_ISNT_WORKING':
        $displayblock = '
                        <form action="changepassword.php" method="post">
                        <fieldset class="login">
                        <legend>Enter your information in the form below to reset your password: </legend>
                        <div><label for="password1">Old Password : </label>
                        <p><input type="password" name="oldpassword" size="40" maxlength="60" /></p> </div>
                        <div><label for="password1">New Password :</label>
                        <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                        <div><label for="password2"><span class="required">*</span>Retype New Password :</label>
                        <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                        <br />
                        </fieldset><br />
                        <br />
                        <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                        </form>
                        ';
        return $displayblock;
     break;
}  // end of switch

} //end of method

When calling the class/switch on my main page, The below code works fine.

$reset = new ResetPassword(); 
$displayblock = $reset->get_display_block('EnterSecretAnswer');

However, when attempting to call THIS_ISNT_WORKING nothing displays. A var_dump on $displayblock returns NULL.

$reset = new ResetPassword();
$displayblock = $reset->get_display_block('THIS_ISNT_WORKING');
var_dump($displayblock);

If I call any of the other cases (the ones omitted previously) it returns the required HTML.

Can anyone see something I am missing here? Any help as usual would be greatly appreciated.

EDIT: I have updated the switch as requested below so that the return statement is outside the switch. The end result is still the same. I have also changed the default case to return a string ("abcd") just to verify that the default case wasn't be called, and it wasnt. Still returning NULL.

You should never use return statement within switch. Basically using return statement once in a function is known as a good practice per my knowledge.

public function get_display_block($displayblock) {
 switch ($displayblock) {
    case 'EnterNewPassword':
         $displayblock = '
                    <form action="resetpassword.php" method="post">
                    <fieldset class="login">
                    <legend>Enter your information in the form below to reset your password: </legend>
                    <div><label for="password1">Password :</label>
                    <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                    <div><label for="password2"><span class="required">*</span>Retype Password :</label>
                    <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                    <br />
                    </fieldset><br />
                    <br />
                    <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                    </form>
                    ';
    break;

   case 'THIS_ISNT_WORKING':
      $displayblock = '
                    <form action="changepassword.php" method="post">
                    <fieldset class="login">
                    <legend>Enter your information in the form below to reset your password: </legend>
                    <div><label for="password1">Old Password : </label>
                    <p><input type="password" name="oldpassword" size="40" maxlength="60" /></p> </div>
                    <div><label for="password1">New Password :</label>
                    <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                    <div><label for="password2"><span class="required">*</span>Retype New Password :</label>
                    <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                    <br />
                    </fieldset><br />
                    <br />
                    <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                    </form>
                    ';
         break;
    }

  return $displayblock;
}

Update

Does this work:

   <?php

   $reset = new Reset;
   $displayblock = $reset->get_display_block('THIS_ISNT_WORKING');
   var_dump($displayblock);

   class Reset{
    public function get_display_block($displayblock) {
    switch ($displayblock) {
       case 'EnterNewPassword':
            $displayblock = '
                <form action="resetpassword.php" method="post">
                <fieldset class="login">
                <legend>Enter your information in the form below to reset your password: </legend>
                <div><label for="password1">Password :</label>
                <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                <div><label for="password2"><span class="required">*</span>Retype Password :</label>
                <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                <br />
                </fieldset><br />
                <br />
                <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                </form>
                ';
       break;

      case 'THIS_ISNT_WORKING':
         $displayblock = '
                <form action="changepassword.php" method="post">
                <fieldset class="login">
                <legend>Enter your information in the form below to reset your password: </legend>
                <div><label for="password1">Old Password : </label>
                <p><input type="password" name="oldpassword" size="40" maxlength="60" /></p> </div>
                <div><label for="password1">New Password :</label>
                <p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>
                <div><label for="password2"><span class="required">*</span>Retype New Password :</label>
                <p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>

                <br />
                </fieldset><br />
                <br />
                <div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>
                </form>
                ';
            break;
       }

     return $displayblock;
   }


}

It returns a non-null response for me.

I found the solution, and embarrassingly the error wasn't in the switch or the way I was calling it. I have a config file which is included in every page (didn't mention as didn't think it was relevant) which I had forgot to add the class.

The thing that confused me for so long is that I wouldn't have thought the other cases in the switch would work either, but they did.... Oh well.

Thanks for everyone that answered.

Use it like this

function get_display_block($displayblock) {
    switch ($displayblock) {
        case 'EnterNewPassword':
            echo '<form action="resetpassword.php" method="post">' .
                 '<fieldset class="login">' .
                 '<legend>Enter your information in the form below to reset your password: </legend>' .
                 '<div><label for="password1">Password :</label>' .
                 '<p><input type="password" name="password1" size="40" maxlength="60" /></p> </div>'.
                  '<div><label for="password2"><span class="required">*</span>Retype Password :</label>'.
                  '<p><input type="password" name="password2" size="40" maxlength="60" /></p>  </div>'.
                  '<br />'.
                  '</fieldset><br />'.
                  '<br />'.
                  '<div align="center"><input type="submit" name="submit" value="Submit My Information!" \></div>'.
                  '</form>';
            break;
      }
}

And just do the same for your other cases. Though doing it like this can be a bit slopping, but you'll just need to use it then on your script by calling <? get_displaying_block($yourString); ?> <? get_displaying_block($yourString); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM