PHP Syntax Errors

When programmers write scripts in PHP, it can be easy to make mistakes. Learning to find and fix mistakes is a key programming skill. No programmer writes scripts without ever including errors, so as long as you learn how to identify any syntax errors in your scripts you can learn to create websites and applications successfully. A number of syntax errors are common when working with PHP.

  1. Missing Semi-Colons

    • Statements in PHP end with semi-colons. The following example code demonstrates a correctly structured PHP statement:

      $num = $a * $b;

      If a script includes this line, but with the semi-colon missing, PHP attempts to carry out the calculation including whatever appears on the following line. This prevents the current line of code from executing correctly, as well as stopping subsequent processing from being carried out. When a PHP script with a missing semi-colon executes, the Web browser sometimes displays an error message relating to a line after the line with the error on it.

    Missing Quotes

    • Developers use quotes in PHP scripts for certain techniques, such as defining strings and writing content to the user's browser. Omitting the closing set of quotes is a common error. The following sample code demonstrates an echo statement containing variables and HTML structures:

      echo "<p>Name: ".$the_name."<br/>Address: ".$the_address."</p>";

      This code combines PHP and HTML syntax, so the likelihood of error is increased. If the script omits any of the quotes, the PHP content may be interpreted as HTML to write to the browser, or the HTML may be interpreted as PHP to process on the server, causing the script to fail. Using a highlighting code editor can help to reduce the chance of this error occurring.

    Incorrect Variable Names

    • When developers create PHP variables, their scripts typically refer to these variables in multiple places. PHP scripts can refer to variables using their names, as in the following example code:

      $title = "My Page";

      echo "<h1>".$title."</h1>";

      The second line refers to the variable declared in the first line using its name. If the programmer types the variable name reference incorrectly in the second line, PHP will not be able to access the value of the variable, so the text content will not appear within the Web browser.

    Missing Braces

    • PHP scripts use a variety of control structures including functions, conditionals and loops. Developers can use braces to define sections of code, as in the following example conditional block:

      if($num<0) {

      $num++;

      echo $num;

      }

      This code dictates that the two lines of code within the block should both execute only if the conditional test returns a true value. If no braces appear, the second line of code within the block will execute regardless of whether the test returns a true or false value, so the syntax error will cause logical problems within the script.

Related Searches:

References

Resources

Comments

Related Ads

Featured