Parse Error in PHP

Parse Error in PHP thumbnail
Fixing parse errors can be stressful but is vital in any development project.

When developers write programming scripts in any language, the likelihood of errors can be high. The PHP scripting language is no exception. Developers use PHP code to build websites and Web applications. Often, PHP scripts contain code in other languages, such as HTML markup for defining webpages and SQL for managing databases. When a PHP site contains an error in the syntax, the webpage may display a message indicating that a parse error has been detected.

  1. Causes

    • Parse errors in PHP are caused by mistakes in code syntax. Developers can include accidental syntax errors in PHP scripts in many ways, such as forgetting to include a semicolon at the end of a statement, as follows:
      echo "hello"
      $my_num=5;

      The first line does not have a semicolon at the end, which may prevent it and subsequent processing from executing correctly. Another common error is forgetting to include closing quotes around a string:
      echo "Here is some text;

      The missing closing quotes around this string will prevent the echo statement and any lines after it from functioning reliably. Often, PHP scripts include SQL code, as follows:
      $data_query="SELECT * FROM my_table WHERE item_name='thing'";

      The use of both double and single quotes, such as those in the SQL code, can make parse errors more likely.

    Effects

    • The effects of parse errors vary. Depending on the severity of an error, it may cause a single line or even an entire script to fail. The PHP interpreter works through the lines of code in a linear fashion, processing one line at a time and sometimes using complex control structures, such as loops and conditional statements. A single syntax error in one PHP script may therefore have a severe impact on an entire site or application.

    Reporting

    • Depending on the server setup for a PHP installation, the Web browser may display error messages that developers can use to locate mistakes. These include parse errors, which often indicate a line number. This line number indicates the line within the script at which the PHP interpreter has detected an error. However, the line number indicated may not be the actual line causing the error, as many syntax errors affect impacting subsequent lines of code. For example, the following syntax error may generate a message indicating the line after the actual error source:
      $some_text = "<p>Here is an HTML paragraph</p>;
      echo $some_text;

      The error is on the first line, which is missing closing quotes. However the parse error message may indicate the second line or even a line later in the script.

    Debugging

    • Debugging is a key skill for all programmers, including those working in PHP. The first step in resolving a parse error is locating its source. The error reporting functions in PHP can be helpful in this task, particularly if they indicate line numbers accurately. Often, developers start from the indicated line number for an error and work back from it until they locate the syntax error causing the problem. Once the problem is located, the developer can make alterations to the script and upload it to the server again before testing to see if the error has been resolved.

Related Searches:

References

Resources

  • Photo Credit Stockbyte/Valueline/Getty Images

Comments

Related Ads

Featured