Easy to make PHP small error and corresponding analysis

Source: Internet
Author: User
When we write programs, even experienced programmers, making a mistake is inevitable. However, if developers fail to notice these errors or understand the meaning of compiler error messages, they are not only useless, but often frustrating.
A better understanding of the error message can greatly save the time it takes to find and correct the wrong content. Therefore, in this article, we will introduce the PHP program often made mistakes, as well as different types of PHP error message and meaning. In addition, the content described in this article is not related to the version of PHP that is used. More PHP learning content, you can refer to the "How to give a good name in the program variables," "24 Useful PHP class library," "PHP array of detailed interpretation."
To figure out why the compiler reports errors on a single line, you must first clarify the compiler's mechanism for parsing PHP code.
Variable declaration
If you declare a variable in a statement, as follows:
$var = ' value ';
The compiler will first find the value of the right half of the statement, which is precisely the part of the statement that often throws an error. If you use an incorrect syntax, a parsing error occurs.

Parsing errors
For example, parse error: Parsing errors, unexpected t_while in C:Program Filesapache groupapachehtdocsscript.php on line 19
Each time a previous error is identified, the parsing error continues to occur one after the other, and PHP stops executing the script after the first parse error. Also, parsing errors have very little information and do not report the line number where the error is. For example, a predefined keyword is used in an expression, such as: while = 10; While is a predefined keyword, it cannot be assigned a value. Predefined keywords include while, function, and so on, and we cannot use these predefined keywords to name variables, otherwise the compiler will make an error. where t_if represents IF (), T_while represents while (), t_for represents for (), and so on.

Common Mistakes
There are also common errors, such as statements that do not use semicolons (;) end, missing quotes in strings, and so on. In addition, no braces (}) are used to end a function or a loop, such as:
function Uselessfunction () {
for ($i < 0; $i < $i + +) {
}
The following error will be generated:
Parse Error:parse error, unexpected $ in C:Program Filesapache groupapachehtdocsereg2.php on line 9
Because the function uselessfunction does not end with curly braces (}), the PHP compiler constantly finds the curly braces that represent the end until the end of the file is reached. Because the compiler does not find a matching brace, it reports an error at the end of the file.
If the hierarchy of the code is correctly reflected, the error message becomes apparent. Otherwise, the code can be very difficult to debug. Therefore, it is important to identify the hierarchy of the code, which is easier for subsequent developers to improve the code.

MySQL Error
Another type of error message is MySQL error, which often makes PHP novice feel quite headache, such as:
Warning:supplied argument is not a valid MySQL result resource in ...
The following line of errors reported above may be:
while ($row = Mysql_fetch_array ($result)) {}
The parameter $result is not a valid resource because the query fails and the mysql_fetch_array cannot be processed. The syntax for either query is invalid or the connection to the database fails and should be tested in the MySQL console.

More mistakes that are easy to make

1. Notice the difference between Echo and print
Both Echo and print in PHP are output, but there are subtle differences between the two. There is no return value after the echo output, but print has a return value and returns Flase when its execution fails. So it can be used as a normal function, for example, "= print" Hello world "; The value of the variable will be 1. And the Echo statement in the code runs faster than the print statement.

2. Note the difference between the empty string (' ') and null
PHP hollow strings and null are stored with a value of 0, but their type is not the same, the former is a string, while the latter is null, the visible string ("'), the null value is equal but the type is unequal.

3. Distinguish between = = (equal to) and = = = (congruent with)
Both are comparison operators, = = (equals) Only compare values for equality, and = = = (all equals) not only compare the values are equal, but also compare the type is equal, it is more strict.

4. Distinguish between include and require
The features of include () and require () are basically the same, but there are some differences in usage, include () is conditional include function, and require () is an unconditional include function. For example, in the following code, if the variable $ A is true, it will contain the file a.php:
if ($a) {
Include ("a.php");
}
and require () is different from include (), regardless of the value of $ A, the following code will contain the file a.php:
if ($a) {
Require ("a.php");
}
In the case of error handling, the include statement is used, and if an include error occurs, the program skips the include statement, although an error message is displayed but the program will continue to execute. However, the Requre statement will prompt for a fatal error.

5. Note the difference between isset and empty
Empty is to determine whether a variable is "empty", and isset is to determine whether a variable has been set.

6. Distinguish self:: the difference from this-->
When accessing a member variable or method in a PHP class, if the referenced variable or method is declared as a const (constant) or static property, then the domain operator must be used::, and if the referenced variable or method is not declared as const or static, Then use the pointer to operator.

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