Jump to content

[SOLVED] switch() - deafult case: break;


soycharliente

Recommended Posts

As far as the break, no you do not have to use it. The break is under each case where you want it to stop executing without it it just runs. But since the default should be your last case or part of a case it is not necessary.

 

A few examples:

 

<?php
$page = 'index.html';

switch ($page) {
     case 'blue.html':    //this will execute if blue or yellow. 
     case 'yellow.html':

     break;
     default:              // this will execute if no data is passed in or index.html
     case 'index.html':

     break;    
}

 

That is a valid switch statement, since the default is associated with the index it will run;

 

<?php
$page = 'index.html';

switch ($page) {
     case 'index.html':

     break;
     case 'yellow.html':
    
     break;
    default:
}

 

Is also valid incase you wanted a special circumstance for the no page

 

<?php
$page = 'index.html';

switch ($page) {
     case 'index.html':

     break;
      case 'blue.html':
          // extra coding if it is blue
     case 'yellow.html':
         // coding if yellow
     break;
    default:

     break;
}

 

Is also valid but the last break is unnecessary. That is valid with the blue and yellow so if they both have the primary principles but you want to do something different if the case is blue as appose to yellow it will allow you to.

 

 

Anyhow hope that helps ya out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.