Check common mistakes in PHP programming, check php programming _ PHP Tutorial

Source: Internet
Author: User
Tags parse error php compiler
Check common mistakes in PHP programming and check php programming. Check common mistakes in PHP programming, check php programming Overview: This article checks PHP developers in encoding, easy to ignore or do not pay attention to small errors and errors. Variable Declaration if you check common errors in PHP programming in one statement, check php programming

Summary: This article describes the minor mistakes and errors that PHP developers can easily ignore or ignore when coding.

Variable Declaration

If you declare a variable in a statement, as shown in the following code: $ var = 'value', the compiler first calculates the value in the right half of the statement, which is precisely the part of the statement that often causes errors. If the syntax used is incorrect, a parsing error occurs.

Parsing error

For example, Parse error: Parsing error, unexpected T_WHILE in c: program filesapache groupapachehtdocsscript. php on line 19 each time a previous error is identified, the parsing error occurs one by one, and PHP stops executing the script after the first parsing error. In addition, parsing errors have very little information, and almost no error row is reported. For example, the expression uses a predefined keyword, for example, while = 10; while is a predefined keyword and cannot be assigned a value. Predefined keywords include while and function. we cannot use these predefined keywords to name variables. Otherwise, the compiler reports an error. T_IF indicates if (), T_WHILE indicates while (), and T_FOR indicates.

Common errors

There are also some common errors, such as the statement does not end with a semicolon (;), and the quotation marks are missing in the string. In addition, the braces (}) are not used to end a function or a loop, for example, function UselessFunction () {for ($ I <0; $ I <10; $ I ++) {} will generate the following error: Parse error: parse error, unexpected $ in c: program filesapache groupapachehtdocsereg2.php on line 9 because the UselessFunction function does not end with braces, the PHP compiler keeps searching for braces indicating the end of the file until it reaches the end of the file. Because the compiler does not find a matching braces, an error is reported at the end of the file. If the hierarchy of the code is correctly reflected, the error information becomes very obvious. Otherwise, code debugging will be very difficult. Therefore, you must mark the code hierarchy, which is easier for subsequent developers to improve the code.

MySQL error

Another type of error message is a MySQL error, which often makes PHP beginners feel a headache, for example: Warning: Supplied argument is not a valid MySQL result resource in... the error line reported above may be: while ($ row = mysql_fetch_array ($ result) {} the parameter $ result is not a valid resource because the query fails, mysql_fetch_array cannot be processed. If the syntax of any query is invalid or the connection to the database fails, you should test it on the MySQL console.

Note the differences between echo and print.

Echo and print are both outputs in PHP, but there are still slight differences between them. No return value after echo output, but print has a return value. if the execution fails, flase is returned. Therefore, it can be used as a common function. for example, execute $ r = print "Hello World"; the value of variable $ r is 1. In addition, the echo statement in the code runs more efficiently than the print statement.

Note the difference between NULL strings ('') and NULL.

PHP empty strings and NULL are both stored as 0, but their types are not the same. The former is string, and the latter is NULL. the visible string ('') and NULL values are equal, but their types are not equal.

Distinguish Between = (equal to) and = (all equal)

Both of them belong to the comparison operator. = (equal to) only compares whether the values are equal, while = (all equal to) not only compares whether the values are equal, but also compares whether the types are equal, it is stricter.

Distinguish include from require

The include () and require () functions are basically the same, but there are also some differences in usage. include () is a conditional include function, while require () is a unconditional include function. For example, in the following code, if the variable $ a is true, it will contain file. php: if ($ a) {include (". php ");} while require () is different from include (). No matter what value $ a gets, the following code will put the file. php contains: if ($ a) {require (". php ");} for error handling, use the include statement. If an include error occurs, the program skips the include statement. Although the include statement displays an error message, the program continues to execute. However, the requre statement prompts a fatal error.

Note the differences between isset and empty.

Empty is used to determine whether a variable is "null", while isset is used to determine whether a variable has been set.

Distinguish self: from this -->

When accessing a member variable or method in the PHP class, if the referenced variable or method is declared as const (constant) or static (static attribute), the domain operator must be used: :. if the referenced variable or method is not declared as const or static, use the pointing operator->.

Summary: This article takes a look at small mistakes and errors that PHP developers can easily ignore or ignore when coding. If the variable is declared in a statement...

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.