![]() Whereas, in the if statement you can use ranges in expression. The switch uses primitive data types while cases use constant values.So it makes compact or easily readable while using switch over many else if statements. The switch is comparable to many nested if statements (else if) in PHP.You should use the switch statement if there are many comparisons.Where should you use the switch case statement?Īs such, the switch case and if else statements are the decision-making statements in PHP: As a programmer, you should know the difference between the switch and if.else statements that where to use each depending on different scenarios. The if statement is also a decision-making statement in PHP and other programming languages. The switch case is the decision-making statement in PHP. The is Month of September! What is the switch case statement? ![]() See the code and output below: $month = date("n") In real programming, this may be quite useful in some scenarios where you may get the current month and perform some action on that basis.Īlso, note that we did not create the default case as there are only twelve possibilities that are also the number of cases used. Then we created twelve cases and whichever is true, it will display the month name. The current month is assigned to a variable that acts as the expression in switch case PHP statement. The month number can be taken by using the “n” date formatting character in the PHP date function. In the following example, we have used the date function to get the number of the current month. The executed case number is: 5 An example of switch case in PHP with Months Whichever case evaluates as true, in that case, its 5, the variable value will be displayed by using the echo statement. ![]() The variable is used as an expression in the switch statement.Īfter that, five cases are created for the constant values of 1 to 5. We simply declared a variable and assigned it a value. This is a basic example of using the PHP switch with 5 cases and a default case. The switch case is among the decision-making statements in PHP.īefore explaining the purpose and difference between the other decision-making statement (if.else) in the later part, let us first look at the syntax and live examples of using this.įollowing is the general way to use PHP switch case statement: Switch (expression) The flow chart of Switch case statement
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |