There are many built-in methods of logging and troubleshooting in PHP. In most cases, PHP data is logged in an error log file using the error_log() function. The error log is disabled by default in PHP. To enable you’ll need to edit the php.ini file or the wp-config.php file if you’re using WordPress.

Logging into the JavaScript console might be easier for you. Here’s the helper function to do that.

/**
 * Write PHP Data in JavaScript console.
 */
function php_logger( $output ) {

    if ( is_array( $output ) ) {
        $output = implode( ',', $output );
    }
        
    // Print the result into the JavaScript console
    echo "<script>console.log( 'PHP LOG: " . $output . "' );</script>";
}

An example in WordPress would be:

add_action( 'admin_init', function() {
	$url = get_option( 'siteurl' );

	php_logger( $url );
});

Here it is, the screenshot.

Log PHP data in JavaScript console
Tagged on:             

Sanjeev Aryal

Don't bury your thoughts, put your vision into reality ~ Bob Marley.

Leave a Reply

Your email address will not be published. Required fields are marked *

× WhatsApp