Print something in PHP built-in web server

前端 未结 3 2110
故里飘歌
故里飘歌 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:39

    The development web server built in to PHP 5.4+ does not work in the way you want. That is, it's not a PHP process, and you can't have it run code for you.

    It's designed to serve PHP applications and content from the specified directory. The output of the server process is the access log. You can write to the log using the error_log function, with a value of 4 as the message_type. So, in theory, you could do something like

    ob_start();
    var_dump($_POST);
    error_log(ob_get_clean(), 4);
    

    It sounds like you're trying to perform some debugging. You should be using real debugging tools instead of cobbling something together.

    0 讨论(0)
  • 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 :

    <?php declare(strict_types=1);
    
    
    /**
     * This is for development purpose ONLY !
     */
    final class ServerLogger {
    
        /**
         * send a log message to the STDOUT stream.
         *
         * @param array<int, mixed> $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);
    
    0 讨论(0)
  • 2020-12-25 13:01

    Just pipe your data to error_log():

    error_log(print_r($_REQUEST, true));
    

    0 讨论(0)
提交回复
热议问题