Go to GoReading for breaking news, videos, and the latest top stories in world news, business, politics, health and pop culture.

Common PHP Bugs

104 4

    Using an Assignment Operator in a Conditional Expression

    • The equal sign is an assignment operator in PHP. For example, the following statement assigns the value five to the variable $x:

      $x = 5;

      Two equal signs together make a conditional operator. For example, the following statement assigns the value three to the variable $y, if the variable $x is equal to five:

      if ($x == 5) $y = 3;

      A common PHP bug is to omit one of the equal signs in the conditional expression. This causes the interpreter to perform an assignment and then a conditional evaluation on the result of the assignment, for which any non-zero value will evaluate to "true." For example, consider the following statement:

      if ($x = 5) $y = 3;

      The variable $y will always be equal to three, because the value five is assigned to, instead of compared to, the variable $x. Since five is non-zero, the result will always evaluate to "true."

    Omitting a Trailing Quote From a String

    • Another common PHP bug occurs when you don't properly close a string with a trailing quote. It's easy to do this, especially if you are mixing single and double quotes in the same string. It's sometimes difficult to track the source of this bug because the error message can be quite different from the real problem, and the interpreter often points to a line that is several lines past where the real problem is. This is because the interpreter continues to try and process the code in spite of the missing quote. Consider the following script:

      <?php
      $string1 = 'And she said, "I wonder why?";
      $string2 = 'And he replied, "I think I know the answer!"';
      if ($plot_path1)
      echo $string1;
      else
      echo $string2;

      This code will generate an "unexpected T_STRING" error that points to the line containing the "if" statement, which is two lines below where the real problem occurs.

    Forgetting the "$" When Switching Between Languages

    • PHP programs often have both HTML markup and JavaScript code interspersed with PHP script. When switching between PHP and JavaScript, it's easy to forget which language you are using and to adopt the standards of the wrong scripting language. A common mistake is to omit the dollar sign in front of a PHP variable, since it is required in PHP but not in JavaScript. For example, the following script omits the dollar sign from the variable "i" in the "for" loop after the JavaScript snippet of code:

      <?php
      $total = sum($parts);
      ?>
      <script type="text/javascript">
      document.getElementById("totalfield").value = <?php echo $total; ?>;
      </script>
      for (i=0; i<25; i++) {
      process(item[i]);
      }
      ?>
      The interpreter points to the line with the error, but issues the error message "unexpected '=', expecting ';' "

    Including a Semicolon After an "If" Line

    • Since most lines in PHP end in a semicolon, a common PHP bug is to include a semicolon after a line with an "if" statement. This can be a difficult bug to track down because it doesn't trigger an error message. For example, the following script will always echo "Low on inventory!" because of the semicolon at the end of the line with the "if" statement. The semicolon effectively eliminates the conditional expression because it terminates the "if" statement without evaluating an expression.

      <?php
      if ($count < 5);
      {
      echo "Low on inventory!";
      }

Source...

Leave A Reply

Your email address will not be published.