PHP analog of console.log () function


/ Published in: PHP
Save to your folder(s)

Debugger analog for PHP: debugger console_log ().


Copy this code and paste it in your HTML
  1. function console_log() {
  2. static $f = false;
  3.     if (!func_num_args()) return; # Argumenty` ne peredany`
  4.     if (!$f) $f = fopen('!console.log',"w");
  5.     foreach (func_get_args() as $arg) {
  6.         if (is_bool($arg)) $s = $arg?'TRUE':'FALSE';
  7.         elseif (is_array($arg) or is_object($arg)) $s = print_r($arg, TRUE);
  8.         else $s = $arg;
  9.         fwrite($f,$s.' '); # vy`vod argumentov razdeliaetsia probelom
  10.     }
  11. }
  12.  
  13. //Primer ispol`zovaniia
  14. # skript vy`polniaetsia, peremenny`e sozdaiutsia
  15. $a = array('name'=>'PHP debagger console_log()', 'txt'=>'Primer ispol`zovaniia');
  16. $b = isset($a); # ?.?. TRUE
  17. $c = time();
  18. # a teper` uznaem, kak u nas dela
  19. console_log('$a',$a,'$b',$b,'? ??? $c',$c);
  20.  
  21. // ========= Variant 2 =============
  22. public function _console_log(){
  23. date_default_timezone_set ("Europe/Kiev");
  24. static $f = false;
  25. if (!func_num_args()) return; # Argumenty` ne peredany`
  26. if (!$f) $f = fopen('!console.log',"w");
  27. foreach (func_get_args() as $arg) {
  28. if (is_bool($arg)) $s = $arg?'TRUE':'FALSE';
  29. elseif (is_array($arg) or is_object($arg)) $s = print_r($arg, TRUE);
  30. else $s = $arg;
  31. $c = date("Y.m.d H:i:s");
  32. fwrite($f,$c . " ". $s.PHP_EOL);
  33. }
  34. }

URL: http://pavluha.net/php-analog-funktsii-console-log.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.