PHP error analysis

Source: Internet
Author: User
Tags php compiler
Even experienced programmers make mistakes in programming. If developers cannot notice these errors or understand the meaning of the compiler error messages, these error messages are not only useless, but often frustrating, therefore, a better understanding of the error information can greatly save the time required to find and correct the error content. Even experienced programmers make mistakes in programming. If developers cannot notice these errors or understand the meaning of the compiler error messages, these error messages are not only useless, but often frustrating, therefore, a better understanding of the error information can greatly save the time required to find and correct the error content.

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
Every 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, braces (}) are not used to end a function or loop, for example:
Function UselessFunction (){
For ($ I <0; $ I <10; $ I ++ ){
}

The following errors will occur:
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 constantly searches for braces indicating the end until 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.

More mistakes

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

  2. Note the differences 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.

  3. Distinguish Between = (equal to) and = (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.

  4. 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 the file a. php:
    If ($ ){
    Include ("a. php ");
    }
    The require () is different from include (). No matter what the value of $ a is, the following code will include the File a. php:
    If ($ ){
    Require ("a. php ");
    }
    For error handling, if an include statement is used, the program will skip the include statement in case of a inclusion error. Although the include statement will display an error message, the program will continue to execute. However, the requre statement prompts a fatal error.

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

  6. Distinguish between self: And 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->.

The above is the details of PHP error analysis. For more information, see other related articles in the first PHP community!

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.