Summary: PHP programming process should pay attention to a variety of considerations

Source: Internet
Author: User
Tags php language variables parse error php programming variable

Recently, PHP has become one of the most popular scripting languages because of its powerful, easy-to-use features that are being loved and accepted by more and more people. This article will be combined with users in the process of using PHP often prone to problems, including syntax errors, program running errors and logic errors, such as detailed introduction, hope to be able to those who are currently learning the PHP language help. In addition, this article will be in the end of the article in the PHP programming process should pay attention to the various issues to be summed up for readers to refer to.

A notable feature of the PHP scripting language is the ability to automatically generate variable instances without requiring users to declare the variables they need to use. This feature is worthy of praise, but at the same time for users to write a script program buried hidden dangers. On the good side, the user no longer needs to declare every variable that will be used in the program at the beginning of the program, as in C, but now the user has to be very careful with the correct spelling of each variable name, or you may inadvertently create a new variable. Some programming languages, such as Perl, can record information about variables that are used only once in a program, and warn you about a user's preset settings. In addition, Perl provides a very powerful debugger. As of version 4.0.2, the PHP language does not contain a debugger. But this does not affect the popularity and promotion of PHP language. PHP offers a wide range of features, even when compared to a mature programming language like C. It can be said that PHP is currently the most convenient, one of the most complete Web support scripting language.

Now, let's get to the bottom of this, and take a look at the problems that are easy to come up with in the PHP programming process, and the corresponding measures to be taken.

The use of semicolons is just like when we write an article we have to add a period at the end of every sentence, PHP requires that every statement in the program must use a semicolon ";" This is the most basic grammatical rule, but it is also the most prone to problems. When we write a program, we rarely check whether the semicolon is missing on one line, but once any negligence occurs, the parser sends out an error report immediately. In some cases, the report may contain the number of statement lines in which the problem occurs.

<?php
$Output = "Hello World";
Echo $Output
$Other = "blah";
Print $SomeMoreText;
?>

At the end of the second line of the code, "Echo $Output," We missed a semicolon, and if you execute the script you will get the following error message:

Parse error:parse error, expecting ', ', ' or '; ' in/usr/local/apache/htdocs/test.php on line 8 the report, while pointing out the cause of the error, omitted the comma "," or Semicolon ";", but the problem statement is set to line eighth. Because this code is very simple, it's easy to find where the error really occurs. However, if the program is very complex, it may be difficult to find a smooth error.

According to the author's previous experience, the following methods are recommended:

If the statement that is indicated in the error report does not have an obvious problem, you can check that the other instruction lines (excluding the comment lines) before the statement are correct. If you still do not find the error, you can comment out the statement line that is indicated in the report (precede the line with the "//" or "#" annotation symbol) or change to another statement where the user can ensure that there is no problem at all. After that, rerun the program, and if the error message still points to the same line, it indicates that the statement that really has the problem should be in front of the statement line that was commented out. Follow the above method to check each line of command before the position, until the error message changes. At this time, we have successfully dug out the real culprit.

The question of variables differs from other programming languages requiring users to explicitly declare variables, and the PHP language allows users to automatically use all variables without having to make a prior declaration. The spelling error of variable names is a big problem for PHP users.

<?php
function Combine ($FirstHalf, $SecondHalf)
{
$Combined _string= $FirstHalf. $SecondHalf;
return $Combined _string;
}
$FirstString = "WDVL-";
$SecondString = "Illustrated Encyclopedia";
$Combine _result=combine ($FirstString, $SecondString);
Print $Combined _result;
?>

When we run the above script, we will see the error prompts that occur because the program does not return any data. Here, we have chosen a very intuitive example for a better explanation of the problem. In reality, sometimes the problem is not so simple. I believe everyone has found the cause of the problem, that is, "Print $Combined _result;" The variable name "$Combined _result" in should be changed to "$Combine _result."



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.