Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse the situation that causes errors in PHP code

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to parse the PHP code errors caused by the situation, the editor feels very practical, so share with you to learn, I hope you can learn something after reading this article, say no more, follow the editor to have a look.

People who are more careful will make mistakes when writing programs. The following is mainly about the analysis of these minor errors in PHP.

These errors often confuse the PHP compiler. If developers cannot understand the meaning of compiler error messages, these error messages are not only useless but often frustrating.

When we write programs, no matter how careful we are, mistakes are inevitable. These errors often confuse the PHP compiler. If developers cannot understand the meaning of compiler error messages, these error messages are not only useless but often frustrating.

When compiling a PHP script, the PHP compiler does everything it can to report the problems it encounters. This creates a problem: only when an error occurs can PHP identify it (which is described in detail later in this article). For this reason, the compiler points out that the line that went wrong may appear to be grammatically correct, or it may be a line that doesn't exist at all!

A better understanding of the error message can greatly save time in identifying and correcting the error content. Therefore, in this article, I will try to clarify many different types of PHP error messages and how to correctly understand the meaning of various error messages in the development process.

To figure out why the compiler reports an error on a line, you must first clarify the mechanism by which the compiler parses PHP code. I'm not going to discuss this in detail in this article, but we'll discuss some simple concepts that are more prone to errors.

Variable declaration

If you declare a variable in a statement, the specific way is as follows:

$variable = 'value'

The compiler first calculates the value of the right half of the statement (that is, everything to the right of the equal sign). In some programming books, this is represented as the RHS of a statement (the right half). It is precisely this part of the statement that often causes errors. If you use incorrect syntax, a parsing error will occur.

Parsing error

Parse error: parsing error, unexpected T_WHILE in c:\ program files\ apache group\ apache\ htdocs\ script.php on line 19

Parsing errors continue to occur one after another each time the previous error is determined. Because PHP stops executing scripts after * * parsing errors, debugging and correcting this series of errors is often particularly tiresome.

Also, parsing errors have little information and hardly report the line number where the error is located. The specific reason is that when an error occurs, the compiler determines that several lines of syntax should look valid until it encounters an invalid syntax, most likely when predefined words are used in the expression, such as

While = 10; / / Bad? While is a predefined word and cannot be assigned to a value

Predefined words include while, function, etc., if PHP uses uses to evaluate your code. You can't use these predefined words to name variables, and if you do, PHP will report more errors, which you can't stand.

On this issue, the following example may be helpful to you. Please consult and read the PHP code shown below:

$b = "somevalue" if ($b = = "somevalue") {print "Hello world!";}? >

The error is on the "$b =" line (missing a semicolon at the end of the statement), so the error should be "parsing error: missing semicolon on line 3", right? Should not be based on the parser:

Parse error: parse error, unexpected T_IF in c:\ program files\ apachegroup\ apache\ htdocs\ ereg2.php on line 4

In line 4, the syntax of the if () statement is correct. So what is the compiler confused by? The clue is the "unexpected T_IF" part. There is a "unexpected tracking error?" When an error occurs, it means that the compiler finds that it appears where the predefined word should not appear. T_IF stands for if (), T_WHILE for while (), T_FOR for for (), and so on.

Fortunately, some of the wrong reasons are simple:

The statement does not end with a semicolon (;), as in the example above. Missing quotation marks in the string.

Other common mistakes

The most common mistake I've seen is one that occurs when you don't end a function or a loop with curly braces (}), which is probably the most common and annoying error. The specific code is as follows:

Function UselessFunction () {for ($I < 0; $I < 10; $iTunes +) {}

The following error will occur:

Parse error: parse error, unexpected $in c:\ program files\ apache group\ apache\ htdocs\ ereg2.php on line 9

Because the function UselessFunction does not end with curly braces (}), the PHP compiler keeps looking for curly braces that indicate the end until the end of the file is reached. Because the compiler cannot find a matching curly braces, it reports an error at the end of the file.

If the hierarchy of the code is correctly reflected, the error message becomes obvious. If you don't indicate the hierarchy of the code, it's almost impossible to find out what you've forgotten. So, remember, be sure to indicate the hierarchy of the code. The Tab key can easily do this. It is also easier for subsequent developers to grasp the code framework and modify it.

MySQL error

Another extremely annoying mismessage is the most common MySQL error, which is often a headache for beginners to PHP:

Warning: Supplied argument is not a valid MySQL result resource in...

The line with errors reported above may be:

While ($row = mysql_fetch_array ($result)) {}

The parameter $result is not a valid resource. In English it means that the mysql_fetch_array cannot be processed because the query failed. The syntax of any query is invalid (you should copy-paste the query to the MySQL console reference for testing), or the connection to the database fails (in which case you should check the user name, password, and so on again).

Prevent errors from happening

* step, the smart encoder can take the following steps to eliminate the following errors:

At the end of each statement, you don't have to think about adding a semicolon-- this should become a habit.

Always indicate the hierarchy of your code as much as possible, which allows you to see if you forgot to add parentheses in places such as if calls or the end of a function.

Use an editor (such as HTML-Kit) that highlights the syntax. With the help of such editors, you can determine if you forget to add quotation marks, lack semicolons, and so on.

The above is how to parse the situation that causes PHP code errors, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report