How to use php default

青灯夜游
Release: 2023-03-17 22:48:01
Original
3460 people have browsed it

In PHP, the default keyword needs to be used together with the switch statement to perform different actions based on different conditions. The switch statement is a conditional selection statement. First, the case value that satisfies the condition will be found as the program entry after execution; if all cases are not satisfied, the default entry will be found. If not found, the entire switch statement will be exited. Default is an alternative entry, but for error or logic checking, the default branch should be added to the switch statement.

How to use php default

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

In PHP, the default keyword needs to be the same as Used together with switch statements.

The switch statement can be used to perform different actions based on different conditions. The switch statement is a conditional selection statement. First, the case value that satisfies the condition will be found as the program entry after execution; if all cases are not satisfied, the default entry will be found. If not found, the entire switch statement will be exited; so default is just a It doesn't matter if you have a backup entrance or not. But for error checking or logic checking, the default branch should still be added to the switch statement.

The switch statement consists of an expression and multiple case labels. The case label is followed by a code block, and the case label serves as the identifier of this code block. The syntax format of the switch statement is as follows:

switch(表达式){
    case 值 1:
        语句块 1;
        break;
    case 值 2:
        语句块 2;
        break;
    ... ...
    case 值 n:
        语句块 n;
        break;
    default:
        语句块 n+1;
}
Copy after login

The switch statement will compare it with the value in the case in turn according to the value of the expression. If they are not equal, continue to search for the next case; if they are equal, the corresponding execution will be performed. statement until the switch statement ends or break is encountered.

Generally speaking, the switch statement ultimately has a default value default. If no matching condition is found in the previous case, the default statement will be executed, similar to the else statement.

The execution flow of the switch statement is shown in the figure below:

How to use php default

Example: Use the date() function to get the English abbreviation of the current week, and print today’s date based on the abbreviation Day of the week

<?php
header("content-type:text/html;charset=utf-8");
$week = date(&#39;D&#39;);
switch($week) {
	case &#39;Mon&#39; :
		echo &#39;星期一&#39;;
		break;
	case &#39;Tue&#39; :
		echo &#39;星期二&#39;;
		break;
	case &#39;Wed&#39; :
		echo &#39;星期三&#39;;
		break;
	case &#39;Thu&#39; :
		echo &#39;星期四&#39;;
		break;
	case &#39;Fri&#39; :
		echo &#39;星期五&#39;;
		break;
	case &#39;Sat&#39; :
		echo &#39;星期六&#39;;
		break;
	case &#39;Sun&#39; :
		echo &#39;星期日&#39;;
		break;
}
?>
Copy after login

How to use php default

How to use php default

Extended knowledge:

You should pay attention to the following when using the switch statement A few points:

  • The difference from the if statement is that the data type of the expression after the switch statement can only be integer or string, not bool. Usually this expression is a variable name. Although PHP is a weakly typed language, the variable in the expression after switch can be any type of data. However, in order to ensure the accuracy of matching execution, it is best to only use one of the integers or strings. type.

  • Different from the if statement, the curly braces after the switch statement are required.

  • The number of case statements is not specified and can be increased indefinitely. But there should be a space between the case label and the value after the case label, and there must be a colon after the value, which is part of the syntax.

  • After the switch matching is completed, the statements in the matched branch modules will be executed one by one, and execution will not stop until the switch structure ends or a break statement is encountered. Therefore, if there is no break statement written after a branch statement, the program will continue to execute the content of the next branch statement.

  • Similar to the else in the if statement, the default label in the switch statement is directly followed by a colon. It seems that there is no condition, but in fact it is conditional. The condition is the "expression" The value cannot be equal to the value after any previous case label, then the statement in the default branch will be executed. The default label is the same as the else clause in if. It is not required in the switch statement and can be omitted.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use php default. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template