PHP programmers often easy 9 mistakes and little knowledge

Source: Internet
Author: User
Tags parse error php compiler
When we have been learning PHP for a period of time, we will develop a certain bad habits, the following is a small compilation of 9 PHP programmers often fan error. For everyone to learn from.

1. Variable declaration

If a variable is declared 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.

2. 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 determined, Parsing errors are repeated 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 and 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.

3. 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, for example: function Uselessfunction () {for ($i <0; $i <10; $i + +) {} will produce the following error: Parse Error:parse Error,unexpected$in C:Program Filesapache groupapachehtdocsereg2.php on line 9 because the function uselessfunction did not end with curly braces (}), The PHP compiler continues to find 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.

4.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 ... One of the lines reported above may be: while ($row =mysql_fetch_array ($result)) {} 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.

5. 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, such as executing $r=print "Hello world"; The value of the variable will be 1. And the Echo statement in the code runs faster than the print statement.

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

distinguish between = = (equals) and = = = (all equals)

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.

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

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

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