How to show debug output in browser console in php?
While debugging php code we might need to use print_r() or var_dump() functions with exit or die, as in following code but by following code page will not render completely because exit or die stop remaining code from execution. If we don’t use die or exit then page design will break.
echo '<pre>'; print_r($debug_array); echo '</pre>'; exit; |
But we can also show debug output in browser console and page render completely without any break at same time. To do so following code will help to show output in browser console.
if(is_array($data) || is_object($data)) { echo("<script>console.log('".json_encode($data)."');</script>"); } else { echo("<script>console.log('".$data."');</script>"); }