Standard input and output for PHP

Source: Internet
Author: User
Tags php language php download sprintf wrappers
This article introduces the content of the PHP standard input and output, has a certain reference value, now share to everyone, the need for friends can refer to

Brief introduction of standard input and output of PHP


7 months ago 956⋅ 11⋅ 0




Today want to use PHP in Sphere online judge to challenge some of the topics, to use the standard input and output, but recently in writing PHP and do not want to use C to write, usually write the project is the form submitted, and really did not consider this, so read the following document.


Documentation here: http://php.net/manual/zh/wrappers.php.php

First, PHP STDIN, STDOUT, stderr Introduction:

STDIN, STDOUT STDERR command input and output stream for input and output to the console (Linux shell terminal, Windows CMD Terminal), which are already open by default and can read and write directly to them, only in the CLI (command-line interface, command-line interface) mode, in HTTP mode, they are undefined.

and their open copyphp://stdinphp://stdoutphp://stderrAlso cannot output content to the HTTP browser, tested: Writephp://stderrContent will be entered into the error log of the default site, and the other two types have no effect.

Stdin/stdout/stderr Introduction:

Raw Stream Stream Open Copy Description
stdin php://stdin standard input, read-only, for input from console;
stdout php://stdout standard output, write-only, to output normal information to the console,
stderr php://stderr error output (standard error), Write-only to output error messages to the console;

It is recommended to use constants STDIN, STDOUT, and STDERR instead of their manually opened copy wrappers php://stdin , php://stdout and php://stderr .

Second, PHP stdin usage:

The PHP language "STDIN" is used to read content from the console, either encountering this constant or fopen() opening the script through a function will php://stdin wait for the user to enter the content until the user presses ENTER to submit.

Write a stdin.php test:

<?phpecho "Please enter content:"; $jimmy = Fgets (STDIN); Echo sprintf ("Input:%s\n", $jimmy); $demo = fopen (' Php://stdin ', ' r '); Echo "Please enter:"; $test = Fread ($demo, 12); Read up to 12 characters echo sprintf ("Input:%s\n", $test); fclose ($demo);

Operation Result:

Please enter the content: Sad input: Sad please input: ASDASDASDASDASDASD input: ASDASDASDASD (here because set up to read up to 12 characters, set more can be fully displayed)

Third, PHP stdout usage:

The PHP language STDOUT is used to output standard information to the console, and the content written to this constant, or to the fopen () function, php://stdout is output directly to the standard output of the console, and the contents of the standard output can be used "> "or"1>"redirects to a specified place, such as a file.

So let's write a file to test it.

<?phpfwrite (STDOUT, "write by STDOUT; \ n"), $demo = fopen ("Php://stdout", "W"), Fwrite ($demo, "write through Php://stdout;"); Fclose ($demo);

Run it:

  Test  php demo.php > a.txt  Test  cat a.txt via stdout, write via php://stdout,%                                                          test  PHP Demo.php written by stdout; Php://stdout write;%                                                          test

Iv. PHP stderr Usage:

The PHP language "STDERR" is used to output error messages to the console, and the content written to the constant, or "Php://stderr" opened to the fopen () function will be output directly to the console's error output, and the contents of the error output can be used " 2>"Redirect to a specified place, such as a file, or you can use"2>&1"to direct the error output to standard output and merge with the standard output.

Let's test it.

<?phpfwrite (STDERR, "STDERR write error output; \ n"); Fwrite (STDOUT, "normal output of STDOUT writes; \ n"); $stdout = fopen ("Php://stdout", "w"); Fwrite ($stdout, "normal output of php://stdout write; \ n"); fclose ($stdout); $stderr = fopen ("Php://stderr", "w"); Fwrite ($stderr, "PHP ://stderr write error output; \ n "); fclose ($stderr);

Normal output and error output are directed to different files:

  Test  php demo.php 1>demo.ok 2>demo.err  test   cat Demo.okstdout The normal output of write, php://stdout write normal output;  Test   Cat Demo.errstderr write error output; php://stderr write error output;

Normal output and error output are combined directed to the unified file:

  Test  php demo.php 1>allinone 2>&1  test  cat Allinonestderr write error output; stdout write normal output; php:// StdOut writes the normal output; Php://stderr writes the error output;

OK, next can go to Sphere Online judge masterful, first test a demo topic

Write a code

<?php    //Your code here$x=0;while ($x!=42) {   $x = fgets (STDIN);   if ($x!=42) {   echo sprintf ("%d\n", $x);}}? >

Submit

Haha, completely OK, no longer limited to Java and C, I can use PHP to challenge some topics, we can also go to try

Related recommendations:

PHP's Smarty more complete notes

PHP Namespaces and auto-loading

PHP Download function

Related Article

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.