How to use php default
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.
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; }
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:
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('D'); switch($week) { case 'Mon' : echo '星期一'; break; case 'Tue' : echo '星期二'; break; case 'Wed' : echo '星期三'; break; case 'Thu' : echo '星期四'; break; case 'Fri' : echo '星期五'; break; case 'Sat' : echo '星期六'; break; case 'Sun' : echo '星期日'; break; } ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
