1
2I am using two ways to debug code
3
4one is print the data and die the function in specific point.
5
6 print_r($data);die;
7
8second one is writing log file in specific point in code.
9
10function write_log($log_msg)
11{
12 $log_filename = "logs";
13 if (!file_exists($log_filename))
14 {
15 mkdir($log_filename, 0777, true);
16 }
17 $log_file_data = $log_filename.'/debug.log';
18 file_put_contents($log_file_data, $log_msg . "\n", FILE_APPEND);
19
20}
21
22write_log("Writing Log");
23$a = array(
24 array('id' => '1','date' => '09-04-2018','length' => '10'),
25 array('id' => '2','date' => '09-04-2018','length' => '20'),
26 array('id' => '1','date' => '10-04-2018','length' => '11')
27 );
28write_log(print_r($a,1));
29
1
2I find it very useful to print out to the browsers console instead of just var_dumping:
3
4function console_log( $data ){
5 echo '<script>';
6 echo 'console.log('. json_encode( $data ) .')';
7 echo '</script>';
8}
9
10Usage:
11$myvar = array(1,2,3);
12console_log( $myvar ); // [1,2,3]
13
1<?php
2$myVar = "hello world!";
3
4var_dump($myVar);
5print_r($myVar);
6
7$allVars = get_defined_vars();
8print_r($allVars);
9debug_zval_dump($allVars);
10
11function sayHello($hello) {
12 echo $hello;
13 debug_print_backtrace();
14}
15
16sayHello($myVar);
17?>
1
2A good example of data output to the console via <script> tags, I myself used this first, but he broke the captcha job, because <script> tags were inserted into the base64 code of the captcha picture. Then I began to display logs in the headers with such a function (it may help someone else, in a similar situation):
3
4<?php
5
6function header_log($data){
7 $bt = debug_backtrace();
8 $caller = array_shift($bt);
9 $line = $caller['line'];
10 $file = array_pop(explode('/', $caller['file']));
11 header('log_'.$file.'_'.$caller['line'].': '.json_encode($data));
12}
13
14?>
15
16Usage:
17$myvar = array(1,2,3);
18header_log( $myvar ); // in headers we see: log_filename_rownumber: [1,2,3]
19