Showing posts with label trace. Show all posts
Showing posts with label trace. Show all posts

2014-08-04

How to debug PHP tips & techniques (Part 3)

Using print statements inside the script to display the trace of execution is a tried and tested method of debugging. For PHP is has several commands that can be used for displaying a trace of the executing PHP file.

Echo command can be used for quick debug message. You can comment them off when not in use.

echo "message line";

// echo "message line"; commented off

USING VAR_DUMP() TO AID YOUR DEBUGGING

var_dump() is a native PHP function which displays structured, humanly readable, information about one (or more) expressions. This is particularly useful when dealing with arrays and objects as var_dump() displays their structure recursively.

echo '<pre>'; var_dump($aMyData); echo '</pre>';

Tip: Wrap var_dump() in <pre> tags to aid readability.

Sometimes you may want to collect the log messages
in separate file instead of showing it in the browse.
You can log data to the standard error log file with error_log. 

http://php.net/manual/en/function.error-log.php

PHP does not come with different debug levels (info, warning, error), you may also 
standalone log modules/classes like Monolog or KLogger.

Monolog: https://github.com/Seldaek/monolog

KLogger: http://codefury.net/projects/klogger/

Besides var_dump(), you could also use var_export and print_r() functions in PHP
to examine variable values.

print_r() displays information about a variable in a way that's readable by humans.

print_r(), var_dump() and var_export() will also show protected and private properties of objects with PHP 5. Static class members will not be shown.



Github CoPilot Alternatives (VSCode extensions)

https://www.tabnine.com/blog/github-copilot-alternatives/