PHP Basics-PHP programming statement-PHP Tutorial

Source: Internet
Author: User
Tags php basics php programming language
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:

 
 
  1. $ Argv ++; // correct
  2. $ Argc --; // correct
  3. $ 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:

 
 
  1. return;
  2. return myDisk.size();
  3. return ($size ? $size : $defaultSize);

4 if And else statements

The if-else statement should have the following format:

 
 
  1. If (condition) {/* condition for operations */
  2. Statements;
  3. }
  4. If (condition) {/* condition for operations .*/
  5. Statements;
  6. } Else {/* conditions for operations */
  7. Statements;
  8. }
  9. If (condition) {/* condition for operations */
  10. Statements;
  11. } Else if (condition) {/* conditions for operations */
  12. Statements;
  13. } Else {/* conditions for operations */
  14. Statements;
  15. }

Note: if statements are always included in "{" and "}" to avoid the following format that may cause errors:

 
 
  1. If (condition) // to avoid this writing, he ignores "{}"
  2. Statement;

The comment format can also be written in the following way:

Reference content is as follows:

 
 
  1. If (condition ){
  2. /* Operation conditions */
  3. Statements;
  4. } Else {
  5. /* Operation conditions */
  6. Statements;
  7. }

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:

 
 
  1. for (initialization; condition; update) {
  2. statements;
  3. }

An empty for statement (all work is being initialized, condition judgment, and updated in the clause) should be in the following format:

 
 
  1. 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:

 
 
  1. while (condition) {
  2. statements;
  3. }

An empty while statement should have the following format:

 
 
  1. while (condition);

7 do... while statement

A do-while statement should have the following format:

Reference content is as follows:

 
 
  1. do {
  2. statements;
  3. } while (condition);

8 switch statement

A switch statement should have the following format:

Reference content is as follows:

 
 
  1. switch (condition) {
  2. case ABC:
  3. /* falls through */
  4. statements;
  5. case DEF:
  6.  statements;
  7.  break;
  8. case XYZ:
  9. statements;
  10. break;
  11. default:
  12. statements;
  13. break;
  14. }

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:

 
 
  1. try {
  2. statements;
  3. } catch (ExceptionClass e) {
  4. statements;
  5. }

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:

 
 
  1. try {
  2. statements;
  3. } catch (ExceptionClass e) {
  4. statements;
  5. } finally {
  6. statements;
  7. }

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.