Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php best debugging functions


I find it very useful to print out to the browsers console instead of just var_dumping:

function console_log( $data ){
  echo '<script>';
  echo 'console.log('. json_encode( $data ) .')';
  echo '</script>';
}

Usage:
$myvar = array(1,2,3);
console_log( $myvar ); // [1,2,3]
Comment

php best debugging functions


I am using two ways to debug code

one is print the data and die the function in specific point.
 
     print_r($data);die;

second one is writing log file in specific point in code.

function write_log($log_msg)
{
    $log_filename = "logs";
    if (!file_exists($log_filename)) 
    {
        mkdir($log_filename, 0777, true);
    }
    $log_file_data = $log_filename.'/debug.log';
  file_put_contents($log_file_data, $log_msg . "
", FILE_APPEND);
    
} 

write_log("Writing Log");
$a = array(
        array('id' => '1','date' => '09-04-2018','length' => '10'),
        array('id' => '2','date' => '09-04-2018','length' => '20'),
        array('id' => '1','date' => '10-04-2018','length' => '11')
    );
write_log(print_r($a,1));
Comment

php best debugging functions


A 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):

<?php

function header_log($data){
  $bt = debug_backtrace();
  $caller = array_shift($bt);
  $line = $caller['line'];
  $file = array_pop(explode('/', $caller['file']));
  header('log_'.$file.'_'.$caller['line'].': '.json_encode($data));
}

?>

Usage:
$myvar = array(1,2,3);
header_log( $myvar ); // in headers we see: log_filename_rownumber: [1,2,3]
Comment

PREVIOUS NEXT
Code Example
Php :: Unable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1 
Php :: php artisan tinker PsyExceptionRuntimeException Unable to create PsySH runtime directory 
Php :: php convert to boolean 
Php :: search string inside array of objects php 
Php :: php print_r 
Php :: replace in php 
Php :: php email attachment and message 
Php :: update role spatie 
Php :: add foreign key in laravel migration 
Php :: laravel npm build production 
Php :: php extract last n words of string 
Php :: php ofreach 
Php :: php conditionally remove element from array 
Php :: laravel tree 
Php :: array_push 
Php :: create laravel project with preferred version : 8 
Php :: in laravel date duration validation rule 
Php :: how to create slug in laravel 
Php :: laravel validate datetime with datetime-local 
Php :: title tag wordpress 
Php :: php array remove keys keep values 
Php :: how to get ip address of pc in php 
Php :: date format in wordpress post 
Php :: bycrypt password php 
Php :: php timestamp to seconds 
Php :: laravel cache remember 
Php :: php convert print_r to array 
Php :: laravel create coma separated string from query 
Php :: Day of Week Using carbon library 
Php :: Instalar Lamp server en Ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =