1

How do I log speed benchmarks to a console in php? I don't want to echo the results for the user to see, but just in some kind of log.

I know that I can get the time elapsed with this:

$first  = new DateTime( '11:35:20' );
$second = new DateTime( '12:00:45' );

$diff = $first->diff( $second );

echo $diff->format( '%H:%I:%S' ); // -> 00:25:25

but I want to log the time, and not echo it. Do I use the error_log?

closed as off-topic by birgire, Rarst Dec 27 '13 at 16:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network." – birgire, Rarst
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I think Stackoverflow would be more appropriate than WPSE, for such pure PHP questions. – birgire Dec 27 '13 at 16:01
2

If you will write inside the console of your browser, then use one of a lot of helpers or use a small custom function in php. The follow function is easy to use and log inside the console of the browser.

if ( ! function_exists( 'debug_to_console' ) ) {
    /**
     * Simple helper to debug to the console
     * 
     * @param  object, array, string $data
     * @return string
     */
    function debug_to_console( $data ) {

        $output = '';
        $output .= 'console.info( \'Debug in Console:\' );';
        $output .= 'console.log(' . json_encode( $data ) . ');';

        echo '<script>' . $output . '</script>';
    }
}

A image say more: enter image description here

Alternative use a library, like ChromePHP for Chromium Project, Webug for many different browsers or FirePHP as example. Is give a lot more helpers in this topic.

If you will work this inside WordPress, then is a fast a easy to use helper the plugin Debug Objects, this plugin have the source from the easy to use funtione above inside the plugin and also the CromePHP library.

Not the answer you're looking for? Browse other questions tagged or ask your own question.