|
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/fabacino/php-debug-functions) |
4 | 4 | [](https://codecov.io/gh/fabacino/php-debug-functions) |
| 5 | + |
| 6 | +A collection of simple debug functions which might be helpful in case a full-fledged debugger is not available. |
| 7 | + |
| 8 | +## Requirements |
| 9 | + |
| 10 | +The following versions of PHP are supported: |
| 11 | + |
| 12 | +* PHP 7.0 |
| 13 | +* PHP 7.1 |
| 14 | +* PHP 7.2 |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +TODO |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +* `dbg`: Print out debug value. |
| 23 | +* `dbgr`: Return debug value. |
| 24 | +* `dbglog`: Log debug value into file. |
| 25 | +* `dbginit`: Initialize debug settings. |
| 26 | + |
| 27 | +The functions accept any value for debugging, as long as it can be passed as a variable. |
| 28 | + |
| 29 | +## Examples |
| 30 | + |
| 31 | +### Print values to standard output |
| 32 | + |
| 33 | +```php |
| 34 | +dbg(123); |
| 35 | +dbg('a string'); |
| 36 | +dbg([ |
| 37 | + 'token' => 'dp83kspo', |
| 38 | + 'is_default' => true |
| 39 | +]); |
| 40 | +``` |
| 41 | + |
| 42 | +``` |
| 43 | +// Output |
| 44 | +123 |
| 45 | +a string |
| 46 | +Array |
| 47 | +( |
| 48 | + [id] => 123 |
| 49 | + [token] => dp83kspo |
| 50 | + [is_default] => true |
| 51 | +) |
| 52 | +``` |
| 53 | + |
| 54 | +When running in a non-CLI environment the ouput is wrapped around `pre` tags for better formatting. |
| 55 | + |
| 56 | +Use `dbginit` or the flag `USE_VARDUMP` in case you prefer `var_dump`: |
| 57 | + |
| 58 | +```php |
| 59 | +dbginit(['use_vardump' => true]); |
| 60 | +dbg(123); |
| 61 | +// or |
| 62 | +dbg(123, Debug::USE_VARDUMP); |
| 63 | +// which is the same as |
| 64 | +dbg(123, 1); |
| 65 | +``` |
| 66 | + |
| 67 | +``` |
| 68 | +// Output |
| 69 | +int(123) |
| 70 | +string(8) "a string" |
| 71 | +array(2) { |
| 72 | + 'token' => string(8) "dp83kspo" |
| 73 | + 'is_default' => bool(true) |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Use `dbginit` or the flag `USE_HTMLENTITIES` in case you want to encode HTML entities: |
| 78 | + |
| 79 | +```php |
| 80 | +dbginit(['use_htmlentities' => true]); |
| 81 | +dbg('<b>important</b>'); |
| 82 | +// or |
| 83 | +dbg('<b>important</b>', Debug::USE_HTMLENTITIES); |
| 84 | +// which is the same as |
| 85 | +dbg('<b>important</b>', 2); |
| 86 | +``` |
| 87 | + |
| 88 | +``` |
| 89 | +// Output |
| 90 | +<b>important</b> |
| 91 | +``` |
| 92 | + |
| 93 | +### Save debug output in variable |
| 94 | + |
| 95 | +```php |
| 96 | +// dbgr is the same as dbg, except that the output is returned instead of printed. |
| 97 | +$dbg = dbgr(123); |
| 98 | +``` |
| 99 | + |
| 100 | +### Log debug output to file |
| 101 | + |
| 102 | +```php |
| 103 | +// dbglog is the same as dbg, except that the output is logged instead of printed. |
| 104 | +dbginit(['log_file' => '/path/to/logfile']); |
| 105 | +dbglog($this->doSomething()); |
| 106 | +``` |
| 107 | + |
| 108 | + Alternatively you can pass your own monolog logger instance: |
| 109 | + |
| 110 | +```php |
| 111 | +$logger = new Logger('channel-name'); |
| 112 | +... |
| 113 | +dbginit(['logger' => $logger]); |
| 114 | +dbglog($this->doSomething()); |
| 115 | +``` |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +The MIT License (MIT). Please see [License File](https://github.com/fabacino/php-debug-functions/blob/master/LICENSE) for more information. |
0 commit comments