PHP built-in Web server explore (ii) Customizing the PHP console output console function

Source: Internet
Author: User
We are developing the server side of the app, and when it comes to the app, it's often necessary to keep track of URL requests and parameter reception in real time.

But PHP does not have a proprietary console output function like Python or Java, Python's print () and Java System.out.println (), PHP echo,print and other functions are used directly php-f Index.php can be printed on the shell screen, but the Cli_server runtime does not meet the debug information that is printed only in the shell and not displayed in the browser.

Use degree Niang search for a long time, on-line PHP built-in server CLI mode of data less, there is no useful information found.

By looking at the official PHP documentation, I finally thought of PHP's standard output stream STDOUT

  php://outputis a write-only data stream that allows you to write to the output buffer in the same way as print and Echo

When running in CLI mode, whenever you write data to stdout, you do not need Echo or print to print to the shell client immediately:

Thus, we can write a custom function to encapsulate the data into the Sdtout standard output stream, which is equivalent to printing the dispatch information you want to display to the shell at any time:

1 /* 2 * CLI mode or built-in server print debug information, not browser output 3 * param fixed $data    parameters can be all data types except objects, such as: strings, arrays, Jason, etc. 4*/5  function Console ($data) {6$stdoutfopen(' php:// StdOut ', ' W '); 7 fwrite ($stdout, Json_encode ($data). " \ n ");   // to print out a clearer format, all data is formatted as a JSON string 8 fclose ($stdout); 9 }

When you need to print the current debug information after each request from the built-in server: Call the console () directly

Like what:

To print all received get or post parameters:

Console ($_get);

Console ($_post);

To print a string:

Console ("Hello PHP console Debug");

No more envy of Python and Java console output in the future, PHP also has a console output function.

The above describes the PHP built-in Web server to explore (ii) The custom PHP console output console function, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.