Home > Backend Development > PHP Tutorial > Exploring PHP's built-in Web Server (2) Customizing the PHP console output console function

Exploring PHP's built-in Web Server (2) Customizing the PHP console output console function

WBOY
Release: 2016-07-29 09:02:01
Original
869 people have browsed it

We are developing the server side of the APP. When jointly debugging with the APP, we usually need to track the reception of URL requests and parameters in real time.

But PHP does not have a proprietary console output function like Python or Java, Python’s print() and Java’s System.out.println(), PHP’s echo, print and other functions can be used directly using php -f index.php When cli_server is running, it can be printed on the shell screen, but when cli_server is running, the debugging information is only printed on the shell and not displayed in the browser.

After searching for a long time using Du Niang, there is very little information on PHP's built-in server cli mode on the Internet, and I can't find any useful information at all.

By consulting PHP official documentation, I finally thought of PHP’s standard output streamSTDOUT

 php://output is 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, as long as data is written to STDOUT, it will be printed to the SHELL client immediately without echo or print:

PHP内置Web Server探究(二)自定义PHP控制台输出console函数

So, we can write a custom function to encapsulate the Inputting data from the sdtout standard output stream is equivalent to printing the scheduling information you want to display to the shell at any time:

<span><span>1</span><span>/*</span><span>2</span><span>* cli模式或者内置server打印调试信息,而不在浏览器输出
</span><span>3</span><span>* param fixed $data    参数可以是除了对象以外的所有数据类型,比如:字符串,数组,jason等
</span><span>4</span><span>*/</span><span>5</span><span>function</span> console(<span>$data</span><span>){
</span><span>6</span><span>$stdout</span> = <span>fopen</span>('php://stdout', 'w'<span>);
</span><span>7</span><span>fwrite</span>(<span>$stdout</span>,json_encode(<span>$data</span>)."\n");   <span>//</span><span>为了打印出来的格式更加清晰,把所有数据都格式化成Json字符串</span><span>8</span><span>fclose</span>(<span>$stdout</span><span>);
</span><span>9</span> }</span>
Copy after login

When you need to print the current debugging information after each request of the built-in Server: call console() directly For example:

Print all received GET or POST parameters:

console($_GET);

console($_POST);

PHP内置Web Server探究(二)自定义PHP控制台输出console函数 Print string:

console("hello php console debug");

PHP内置Web Server探究(二)自定义PHP控制台输出console函数No longer need to envy the console output of Python and Java, PHP also has a console output function from now on

The above introduces the exploration of PHP's built-in Web Server (2) Customizing the PHP console output console function, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template