Print something in PHP built-in web server

前端 未结 3 2122
故里飘歌
故里飘歌 2020-12-25 12:05

In python built-in web server when use print in function, it prints result in terminal ...

for example:

Django version 1.3.4, using sett         


        
3条回答
  •  余生分开走
    2020-12-25 12:57

    php built-in server writes output to the php://stdout stream , which mean you can output anything to it, but this should only be used for debugging.

    here's a quick example of how can you achieve the result of writing to the server console :

     $args
         *
         * @return void
         */
        public static function log(...$args): void {
            foreach ($args as $arg) {
                if (is_object($arg) || is_array($arg) || is_resource($arg)) {
                    $output = print_r($arg, true);
                } else {
                    $output = (string) $arg;
                }
    
                fwrite(STDOUT, $output . "\n");
             }
        }
    }
    
    // usage example : 
    ServerLogger::log('Hello, world!');
    // outputting an array : 
    ServerLogger::log($_SERVER);
    

提交回复
热议问题