PHP Basics: describes PHP programming statements. The following describes the programming statements in the PHP programming language. 1. a simple statement contains at most one statement per line. for example, the following is the referenced content: $ argv ++; correct $ argc --; which is introduced below
PHPProgramming statements in the programming language.
1. simple statement
Each line contains at most one statement, for example:
Reference content is as follows:
- $ Argv ++; // correct
- $ Argc --; // correct
- $ Argv ++; $ argc --; // error
2 Compound statements
A compound statement is a sequence of statements contained in braces, such as "{statement }". For example, the following sections.
- The statements included in the statement should be indented to one level than the compound statement.
- The left braces "{" should be at the end of the row starting with the compound statement; the right braces "}" should start with another line and align with the first line of the compound statement.
- Braces can be used for all statements, including a single statement, as long as these statements are part of an if-else or for control structure. This makes it easy to add statements without worrying about introducing bugs due to missing brackets.
3. return statement
A return statement with a return value does not use parentheses "()" unless they make the return value more explicit in some way. For example:
Reference content is as follows:
- return;
- return myDisk.size();
- return ($size ? $size : $defaultSize);
4 if And else statements
The if-else statement should have the following format:
- If (condition) {/* condition for operations */
- Statements;
- }
- If (condition) {/* condition for operations .*/
- Statements;
- } Else {/* conditions for operations */
- Statements;
- }
- If (condition) {/* condition for operations */
- Statements;
- } Else if (condition) {/* conditions for operations */
- Statements;
- } Else {/* conditions for operations */
- Statements;
- }
Note: if statements are always included in "{" and "}" to avoid the following format that may cause errors:
- If (condition) // to avoid this writing, he ignores "{}"
- Statement;
The comment format can also be written in the following way:
Reference content is as follows:
- If (condition ){
- /* Operation conditions */
- Statements;
- } Else {
- /* Operation conditions */
- Statements;
- }
As long as the relationship between branches can be clearly described, where to write comments can be
5. for statement
A for statement should have the following format:
Reference content is as follows:
- for (initialization; condition; update) {
- statements;
- }
An empty for statement (all work is being initialized, condition judgment, and updated in the clause) should be in the following format:
- for (initialization; condition; update);
When a comma is used in the initialization or update clause of a for statement, the complexity is increased because more than three variables are used. If necessary, separate statements can be used before a for loop (initialization clause) or at the end of a for loop (update clause.
6 while statement
A while statement should have the following format
Reference content is as follows:
- while (condition) {
- statements;
- }
An empty while statement should have the following format:
- while (condition);
7 do... while statement
A do-while statement should have the following format:
Reference content is as follows:
- do {
- statements;
- } while (condition);
8 switch statement
A switch statement should have the following format:
Reference content is as follows:
- switch (condition) {
- case ABC:
- /* falls through */
- statements;
- case DEF:
- statements;
- break;
- case XYZ:
- statements;
- break;
- default:
- statements;
- break;
- }
When a case is executed down (because there is no break statement), comments should be added at the position of the break statement. The above sample code contains comments/* falls through */.
9 try... catch statement
A try-catch statement should have the following format:
Reference content is as follows:
- try {
- statements;
- } catch (ExceptionClass e) {
- statements;
- }
A try-catch statement may be followed by a finally statement. it will be executed no matter whether the try code block is successfully executed.
Reference content is as follows:
- try {
- statements;
- } catch (ExceptionClass e) {
- statements;
- } finally {
- statements;
- }
I hope this article will help you.
Programming statements in the PHP programming language. 1. a simple statement contains at most one statement per line. for example, the following is the referenced content: $ argv ++; // correct $ argc --; // positive...