1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 5.00 out of 5)

Php 7 Switch Statement Case Break Lesson 18

Php 7 Switch Statement operates a lot like IF Statements. Generally, you will only use switch statements when comparing one condition. This is what it was designed for and that is how we will use it. If we have multiple conditions to consider then use the IF Else Statement Operator.

Here are the files for this lesson.

What Is a Php 7 Switch Statement *

A Php 7 Switch Statement evaluates one variable. If that one variable is matched it shows results accordingly. Switch Statements are less coding than IF Statement. Here is an example of a Php 7 Switch Statement.

$var=0;

switch ($var) {
case 0:
echo "i equals 0";
break;

}

In the example above we set a variable of zero. Then we start out by typing “switch” and next to that we open and close parentheses. Inside those parentheses we insert our variable. After the parentheses , open and close curly brackets. Inside our curly brackets begin our “Case Operators“. These Case Operators are looking for a match. If they find one, they will display whatever is in the echo String Function. Finally to end the case operators job we tell it to “break“.

Subscribe To My YouTube Channel:

Switch Statement Default Option *

There is a “Switch Statement Default Option” in case we want to show something by default. This is used in case nothing matches the variable. Here is a code example.

//SWITCH STATEMENT DEFAULT OPTION
$var=3;
switch ($var) {
case 0:
echo "i equals 0";
break;
default:
echo "You do not have a match";
}

Using Multiple Options With Case Operator *

You may use multiple options in a Php 7 Switch Statement. The “Case Operator” is responsible for this. Once it finds a match then it will display that match. Here is a code example of using multiple options with the Case Operator.

/*
SWITCH STATEMENT DEFAULT OPTION
WITH MULTIPLE OPTIONS
*/
$var=3;
switch ($var) {
case 0:
echo "Variable equals 0";
break;
case 1:
echo "Variable equals 0";
break;
case 2:
echo "Variable equals 0";
break;
default:
echo "You do not have a matching variable";
}

Illustration Of Using Switch Statement In Php7 *

As an illustration of how and why you would want to use a switch statement in Php 7. Let’s say that you want to display something for each day of the week. This may be an inspirational quote or a joke, maybe both. Here is how something like this can be done using the Switch Statements in Php.

/*
SWITCH STATEMENT 
WITH MULTIPLE OPTIONS
DAYS OF THE WEEK
*/
//$var=date('l');;
$var="Monday";
switch ($var) {
    case "Monday":
        echo "<p>If Monday had a face I would punch it.</p> ";
        break;
    case "Tuesday":
        echo "<p>Tuesday isn't so bad, it's a sign that I survived Monday! </p>";
        break;
    case "Wednesday":
        echo "<p><img src='wednesday.jpg' /></p> ";
        break;
    case "Thursday":
        echo "<p>Wednesday is hump day which makes thursday 'I wonder if he'll call' day.</p> ";
        break;
    case "Friday":
        echo "<p>Friday is like a superhero that always arrives just in time to stop me from savagely beating one of my coworkers with a keyboard. </p>";
        break;
    case "Saturday":
        echo "<p>Q: Why do brunettes know so many blonde jokes?</p>
                    <p>A: It gives them something to do on Saturday night.</p> ";
        break;
    case "Sunday":
        echo "Wherever you go, no matter what the weather, always bring your own sunshine.";
        break;
    default: 
        echo "<p>Please Enter A day Of The Week</p>";
}

In the example above we also used the Php 7 Function of date(‘l’). This will give us the day of the week. Notice as well that we used an image tag on Wednesday.

Advanced Features in The Php 7 Switch Statement *

You can also use arrays for your variables, we will cover this in a future lesson. I hope I was able to give you a basic understand of the Php 7 Switch Statement and how it works. Please share and subscribe for more awesome tutorials.

Video For This Lesson *

Php 7 Switch Statement Case Break Lesson 18 was last modified: November 23rd, 2018 by Maximus Mccullough

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips

Tips

Php 7 Switch Statement Case Break

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.