how to debug php
phpinfo();
how to debug in php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
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]
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 . "\n", 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));
debug php
// ------ string -------
// use echo, exit for output
$a = 123
echo ($a);
exit;
// ------ array -------
$array = array(
1 => 'apple',
2 => 'banana',
)
// Method 1
echo "<pre>";
print_r($array);
echo "</pre>";
// Method 2
echo "<pre>";
var_dump($array);
echo "</pre>";
// Method 3 (no need exit) beacause of dd is already exit
dd($array)
// Method 4 (cakephp)
pr ($array);
// ------ json -------
$json = {"a":"123"}
pr (json_encode($json));
exit;
print_r (json_encode($json));
exit;
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us