Difference between revisions of "PHP/debugging"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP
Jump to navigation Jump to search
(Created page with "=Debugging PHP= ==Blank Pages== This code will cause most errors to generate page output, rather than just a blank page: <syntaxhighlight lang=php> $fErrLevel = E_ALL; error_r...")
 
(fx)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=Debugging PHP=
 
=Debugging PHP=
 +
* See [[/interactive]] for information about step-through debugging.
 
==Blank Pages==
 
==Blank Pages==
 
This code will cause most errors to generate page output, rather than just a blank page:
 
This code will cause most errors to generate page output, rather than just a blank page:
Line 13: Line 14:
 
  php -l {{arg|name of PHP file}}
 
  php -l {{arg|name of PHP file}}
  
==Newbie Traps & Pitfalls==
+
==Traps & Pitfalls==
 
PHP will let you get away with a lot of syntax mistakes which are perfectly valid code (often creating unexpected variables in the process) but not what you intended.  
 
PHP will let you get away with a lot of syntax mistakes which are perfectly valid code (often creating unexpected variables in the process) but not what you intended.  
  
Line 25: Line 26:
 
*** The "is equal to" comparison operator is "==", not "=".
 
*** The "is equal to" comparison operator is "==", not "=".
 
**** You can, however, use the assignment operator ("=") within an expression, and the expression will evaluate to the result of the final assignment.
 
**** You can, however, use the assignment operator ("=") within an expression, and the expression will evaluate to the result of the final assignment.
 +
===undeclared variables===
 +
One of the few major weaknesses of PHP as a serious programming language is its lack of any way to require variable declaration before usage -- the equivalent of "use strict;" in [[Perl]] or "Option Explicit" in [[Visual Basic]]. [http://www.wellho.net/forum/Writing-PHP/use-strict.html Here] is a brief discussion of this problem, with a workaround which might be useful in some situations (but not if you need to turn off warnings for other reasons, such as needing to suppress messages about duplicate http headers).
 
===obsolete===
 
===obsolete===
 
These were true in earlier versions of PHP, but behavior was changed in PHP 5 or maybe 7:
 
These were true in earlier versions of PHP, but behavior was changed in PHP 5 or maybe 7:
* <s>If you pass an object to a function, the function will be operating on a ''copy'' of the object unless the function is called with the object passed as a reference: '''''CalledFunction''(&$''objName'');'''. The function declaration itself needs no modifications.</s>
+
* <s>If you pass an object to a function, the fundebug_print_backtrace()ction will be operating on a ''copy'' of the object unless the function is called with the object passed as a reference: '''''CalledFunction''(&$''objName'');'''. The function declaration itself needs no modifications.</s>
 
* <s>If the function is expected to store the object for later use (e.g. it is a class constructor), the function must also use a reference when saving the object: '''$this->''localName'' =& $''iObjectParam'';'''. Otherwise (again) it will be using a copy, not the original.</s>
 
* <s>If the function is expected to store the object for later use (e.g. it is a class constructor), the function must also use a reference when saving the object: '''$this->''localName'' =& $''iObjectParam'';'''. Otherwise (again) it will be using a copy, not the original.</s>
 +
==Functions==
 +
* [https://www.php.net/manual/en/function.debug-backtrace.php <code>debug_backtrace()</code>]
 +
* [https://www.php.net/manual/en/function.debug-print-backtrace.php <code>debug_print_backtrace()</code>]
 +
==Error Messages==
 +
''some of these are not found in the PHP documentation''
 +
* '''REG_EMPTY''': [[regex]]-related, but not sure what it means; it may reflect a difference between Perl-style regex (preg_* functions) and grep-style regex (ereg* functions). I changed the expression "('|")" to "['"]", and this seemed to solve the problem.
 +
* '''REG_ERANGE''': This error appears to originate in the [[regex]] library, and means "Invalid character range, e.g. ending point is earlier in the collating order than the starting point." (Documented [http://www.laurikari.net/tre/api.html here])
 +
** In one case, this was caused by having a forward-slash ("/") in the bracketed list of possible characters; escaping the forward slash fixed the problem.

Latest revision as of 00:24, 19 August 2024

Debugging PHP

  • See /interactive for information about step-through debugging.

Blank Pages

This code will cause most errors to generate page output, rather than just a blank page:

$fErrLevel = E_ALL;
error_reporting($fErrLevel);
if (!ini_get('display_errors')) {
    ini_set('display_errors', 'TRUE');
}

Some errors will crash PHP completely, causing a blank page even with error_reporting turned on; this command (h/t) will show even those:

php -l <name of PHP file>

Traps & Pitfalls

PHP will let you get away with a lot of syntax mistakes which are perfectly valid code (often creating unexpected variables in the process) but not what you intended.

Here are some mistakes I made when re-learning PHP in 2005 after not using it since 1997, most of which produced no immediate error messages; the code simply wouldn't work, and it took me several edit-upload-run cycles to find each problem:

  • Classes:
    • Member vars and functions must always be referred to using $this->FunctionName()
    • However, var members do not take a $ prefix: $this->varName
    • In a child function (class child extends parent), if the parent has member variables declared as "private" and you attempt to access them with $this->varname, PHP will silently create new member variables for the child scope rather than referencing the old ones or generating an error. (Change the var declarations to "protected".)
  • Operators:
    • As in C/C++:
      • The "is equal to" comparison operator is "==", not "=".
        • You can, however, use the assignment operator ("=") within an expression, and the expression will evaluate to the result of the final assignment.

undeclared variables

One of the few major weaknesses of PHP as a serious programming language is its lack of any way to require variable declaration before usage -- the equivalent of "use strict;" in Perl or "Option Explicit" in Visual Basic. Here is a brief discussion of this problem, with a workaround which might be useful in some situations (but not if you need to turn off warnings for other reasons, such as needing to suppress messages about duplicate http headers).

obsolete

These were true in earlier versions of PHP, but behavior was changed in PHP 5 or maybe 7:

  • If you pass an object to a function, the fundebug_print_backtrace()ction will be operating on a copy of the object unless the function is called with the object passed as a reference: CalledFunction(&$objName);. The function declaration itself needs no modifications.
  • If the function is expected to store the object for later use (e.g. it is a class constructor), the function must also use a reference when saving the object: $this->localName =& $iObjectParam;. Otherwise (again) it will be using a copy, not the original.

Functions

Error Messages

some of these are not found in the PHP documentation

  • REG_EMPTY: regex-related, but not sure what it means; it may reflect a difference between Perl-style regex (preg_* functions) and grep-style regex (ereg* functions). I changed the expression "('|")" to "['"]", and this seemed to solve the problem.
  • REG_ERANGE: This error appears to originate in the regex library, and means "Invalid character range, e.g. ending point is earlier in the collating order than the starting point." (Documented here)
    • In one case, this was caused by having a forward-slash ("/") in the bracketed list of possible characters; escaping the forward slash fixed the problem.