PHP TutorialCompile PHP ExtensionsContributing to the PHP CoreContributing to the PHP ManualCreate PDF files in PHPInstalling a PHP environment on WindowsPHP Alternative Syntax for Control StructuresPHP APCuPHP Array iterationPHP ArraysPHP Asynchronous programmingPHP Autoloading PrimerPHP BC Math (Binary Calculator)PHP Built in serverPHP CachePHP Classes and ObjectsPHP ClosurePHP Coding ConventionsPHP Command Line Interface (CLI)PHP CommentsPHP Common ErrorsPHP Compilation of Errors and WarningsPHP Composer Dependency ManagerPHP ConstantsPHP Control StructuresPHP CookiesPHP CryptographyPHP DateTime ClassPHP DebuggingPHP Dependency InjectionPHP Design PatternsPHP Docker deploymentPHP Exception Handling and Error ReportingPHP Executing Upon an ArrayPHP File handlingPHP Filters & Filter FunctionsPHP Functional ProgrammingPHP FunctionsPHP GeneratorsPHP Headers ManipulationPHP How to break down an URLPHP How to Detect Client IP AddressPHP HTTP AuthenticationPHP Image Processing with GDPHP ImagickPHP IMAPPHP Installing on Linux/Unix EnvironmentsPHP JSONPHP LocalizationPHP LoopsPHP Machine learningPHP Magic ConstantsPHP Magic MethodsPHP Manipulating an ArrayPHP mongo-phpPHP Multi Threading ExtensionPHP MultiprocessingPHP MySQLiPHP MySQLi affected rows returns 0 when it should return a positive integerPHP NamespacesPHP Object SerializationPHP OperatorsPHP Output BufferingPHP Outputting the Value of a VariablePHP Parsing HTMLPHP Password Hashing FunctionsPHP PDOPHP PerformancePHP PHPDocPHP Processing Multiple Arrays TogetherPHP PSRPHP Reading Request DataPHP RecipesPHP ReferencesPHP ReflectionPHP Regular Expressions (regexp/PCRE)PHP Secure Remeber MePHP SecurityPHP Sending EmailPHP SerializationPHP SessionsPHP SimpleXMLPHP SOAP ClientPHP SOAP ServerPHP SocketsPHP SPL data structuresPHP SQLite3PHP StreamsPHP String formattingPHP String Parsing

PHP Debugging

From WikiOD

Dumping variables[edit | edit source]

The var_dump function allows you to dump the contents of a variable (type and value) for debugging.


$array = [3.7, "string", 10, ["hello" => "world"], false, new DateTime()];


array(6) {
  string(6) "string"
  array(1) {
    string(5) "world"
  object(DateTime)#1 (3) {
    string(26) "2016-07-24 13:51:07.000000"
    string(13) "Europe/Berlin"

Displaying errors[edit | edit source]

If you want PHP to display runtime errors on the page, you have to enable display_errors, either in the php.ini or using the ini_set function.

You can choose which errors to display, with the error_reporting (or in the ini) function, which accepts E_* constants, combined using bitwise operators.

PHP can display errors in text or HTML format, depending on the html_errors setting.


ini_set("display_errors", true);
ini_set("html_errors", false); // Display errors in plain text
error_reporting(E_ALL & ~E_USER_NOTICE); // Display everything except E_USER_NOTICE

trigger_error("Pointless error"); // E_USER_NOTICE
echo $nonexistentVariable; // E_NOTICE
nonexistentFunction(); // E_ERROR

Plain text output: (HTML format differs between implementations)

Notice: Undefined variable: nonexistentVariable in /path/to/file.php on line 7

Fatal error: Uncaught Error: Call to undefined function nonexistentFunction() in /path/to/file.php:8
Stack trace:
#0 {main}
  thrown in /path/to/file.php on line 8

NOTE: If you have error reporting disabled in php.ini and enable it during runtime, some errors (such as parse errors) won't be displayed, because they occured before the runtime setting was applied.

The common way to handle error_reporting is to enable it fully with E_ALL constant during the development, and to disable publicly displaying it with display_errors on production stage to hide the internals of your scripts.

phpinfo()[edit | edit source]

Warning[edit | edit source]

It is imperative that phpinfo is only used in a development environment. Never release code containing phpinfo into a production environment

Introduction[edit | edit source]

Having said that, it can be a useful tool in understanding the PHP environment (OS, configuration, versions, paths, modules) in which you are working, especially when chasing a bug. It is a simple built in function:


It has one parameter $what that allows the output to be customized. The default is INFO_ALL, causing it to display all information and is commonly used during development to see the current state of PHP.

You can pass the parameter INFO_* constants, combined with bitwise operators to see a customized list.

You can run it in the browser for a nicely formatted detailed look. It also works in PHP CLI, where you can pipe it into less for easier view.

Example[edit | edit source]


This will display a list of PHP directives (ini_get), environment ($_ENV) and predefined variables.

Xdebug[edit | edit source]

Xdebug is a PHP extension which provides debugging and profiling capabilities.

It uses the DBGp debugging protocol.

There are some nice features in this tool:

  • stack traces on errors
  • maximum nesting level protection and time tracking
  • helpful replacement of standard var_dump() function for displaying variables
  • allows to log all function calls, including parameters and return values to a file in different formats
  • code coverage analysis
  • profiling information
  • remote debugging (provides interface for debugger clients that interact with running PHP scripts)

As you can see this extension is perfectly suited for development environment. Especially remote debugging feature can help you to debug your php code without numerous var_dump's and use normal debugging process as in C++ or Java languages.

Usually installing of this extension is very simple:

pecl install xdebug # install from pecl/pear

And activate it into your php.ini:


In more complicated cases see this instructions

When you use this tool you should remember that:

XDebug is not suitable for production environments

phpversion()[edit | edit source]

Introduction[edit | edit source]

When working with various libraries and their associated requirements, it is often necessary to know the version of current PHP parser or one of it's packages.

This function accepts a single optional parameter in the form of extension name: phpversion('extension'). If the extension in question is installed, the function will return a string containing version value. However, if the extension not installed FALSE will be returned. If the extension name is not provided, the function will return the version of PHP parser itself.

Example[edit | edit source]

print "Current PHP version: " . phpversion();
// Current PHP version: 7.0.8

print "Current cURL version: " . phpversion( 'curl' );
// Current cURL version: 7.0.8
// or
// false, no printed output if package is missing

Error Reporting (use them both)[edit | edit source]

// this sets the configuration option for your environment
ini_set('display_errors', '1');

//*1 will allow all errors to be reported