Can php print to console

Though this is an old question, I've been looking for this. Here's my compilation of some solutions answered here and some other ideas found elsewhere to get a one-size-fits-all solution.

CODE :

    // Post to browser console
    function console[$data, $is_error = false, $file = false, $ln = false] {
        if[!function_exists['console_wer']] {
            function console_wer[$data, $is_error = false, $bctr, $file, $ln] {
                echo '
'.''.[[$is_error!==false] ? 'if[typeof phperr_to_cns === \'undefined\'] { var phperr_to_cns = 1; document.addEventListener["DOMContentLoaded", function[] { setTimeout[function[]{ alert["Alert. see console."]; }, 4000]; }]; }' : ''].' console.group["PHP '.[[$is_error] ? 'error' : 'log'].' from "+window.atob["'.base64_encode[[[$file===false] ? $bctr['file'] : $file]].'"]'.[[[$ln!==false && $file!==false] || $bctr!==false] ? '+" on line '.[[$ln===false] ? $bctr['line'] : $ln].' :"' : '+" :"'].']; console.'.[[$is_error] ? 'error' : 'log'].'['.[[is_array[$data]] ? 'JSON.parse[window.atob["'.base64_encode[json_encode[$data]].'"]]' : '"'.$data.'"'].']; console.groupEnd[];
'; return true; } } return @console_wer[$data, $is_error, [[$file===false && $ln===false] ? array_shift[debug_backtrace[]] : false], $file, $ln]; } //PHP Exceptions handler function exceptions_to_console[$svr, $str, $file, $ln] { if[!function_exists['severity_tag']] { function severity_tag[$svr] { $names = []; $consts = array_flip[array_slice[get_defined_constants[true]['Core'], 0, 15, true]]; foreach [$consts as $code => $name] { if [$svr & $code] $names []= $name; } return join[' | ', $names]; } } if [error_reporting[] == 0] { return false; } if[error_reporting[] & $svr] { console[severity_tag[$svr].' : '.$str, true, $file, $ln]; } } // Divert php error traffic error_reporting[E_ALL]; ini_set["display_errors", "1"]; set_error_handler['exceptions_to_console'];

TESTS & USAGE :

Usage is simple. Include first function for posting to console manually. Use second function for diverting php exception handling. Following test should give an idea.

    // Test 1 - Auto - Handle php error and report error with severity info
    $a[1] = 'jfksjfks';
    try {
          $b = $a[0];
    } catch [Exception $e] {
          echo "jsdlkjflsjfkjl";
    }

    // Test 2 - Manual - Without explicitly providing file name and line no.
          console[array[1 => "Hi", array["hellow"]], false];
    
    // Test 3 - Manual - Explicitly providing file name and line no.
          console[array[1 => "Error", array[$some_result]], true, 'my file', 2];
    
    // Test 4 - Manual - Explicitly providing file name only.
          console[array[1 => "Error", array[$some_result]], true, 'my file'];
    

EXPLANATION :

  • The function console[$data, $is_error, $file, $fn] takes string or array as first argument and posts it on console using js inserts.

  • Second argument is a flag to differentiate normal logs against errors. For errors, we're adding event listeners to inform us through alerts if any errors were thrown, also highlighting in console. This flag is defaulted to false.

  • Third and fourth arguments are explicit declarations of file and line numbers, which is optional. If absent, they're defaulted to using the predefined php function debug_backtrace[] to fetch them for us.

  • Next function exceptions_to_console[$svr, $str, $file, $ln] has four arguments in the order called by php default exception handler. Here, the first argument is severity, which we further crosscheck with predefined constants using function severity_tag[$code] to provide more info on error.

NOTICE :

  • Above code uses JS functions and methods that are not available in older browsers. For compatibility with older versions, it needs replacements.

  • Above code is for testing environments, where you alone have access to the site. Do not use this in live [production] websites.

SUGGESTIONS :

  • First function console[] threw some notices, so I've wrapped them within another function and called it using error control operator '@'. This can be avoided if you didn't mind the notices.

  • Last but not least, alerts popping up can be annoying while coding. For this I'm using this beep [found in solution : //stackoverflow.com/a/23395136/6060602] instead of popup alerts. It's pretty cool and possibilities are endless, you can play your favorite tunes and make coding less stressful.

Can PHP write to console?

log[] and the json_encode[] Function to Write to the Console in PHP. We can use the json_encode[] function along with the JavaScript console. log[] to write to the console in PHP. The json_ecode[] function converts the given associative array into a JSON object and indexed array into a JSON array.

Where does PHP print to?

The docs say that these print to php://output . They are both language constructs, but at a guess, the difference is that print is an expression, but echo is a statement. printf and many friends.

How do you print to the console?

You should use the console. log[] method to print to console JavaScript. The JavaScript console log function is mainly used for code debugging as it makes the JavaScript print the output to the console. To open the browser console, right-click on the page and select Inspect, and then click Console.

Does print work in PHP?

The PHP print Statement The print statement can be used with or without parentheses: print or print[] .

Chủ Đề