Now that you are comfortable with the idea of integrating PHP directly with your other tools, I want to look at the CLI SAPI streams that are opened for use in all CLI scripts. Owing to the fact that it is very common to use stdin , stdout , and stderr inside shell scripts, the process of opening and closing streams to access these has now been automated through the use of the constants STDIN, STDOUT, and STDERR respectively.
These constants, when combined with basic I/O functions like fgets() and fputs(), allow you to easily read and write to the console. fputs() writes to a given stream, and takes two parameters: the stream to write to and the string it should write. It also takes an optional third parameter that is the length of the string to write, but this can be omitted if you wish to write the entire string. It is opposite, fgets(), reads data in from a stream, and takes one parameter: the stream it should read data from. It also has an optional length parameter, to specify how much data should be read. If you do not specify this parameter, the length defaults to 1024 bytes. The return value of fgets() is the string that was read in from the stream.
The concept of streams, which is relatively new in PHP, is a unified approach to handling files, pipes, sockets, and other I/O resources. What this means for us as developers is that fputs() and fgets() can read from and write to files, HTTP connections, or the console - all using the same command. Here is an example script to demonstrate fputs() and fgets() in use for working with the console:
#!/usr/local/bin/php <?php fputs(STDOUT, "\nThe Amazing Favourite Colour Script\n"); fputs(STDOUT, "What is your favourite colour? "); $sometext = strtolower(trim(fgets(STDIN, 256))); fputs(STDOUT, "Your favourite colour is $sometext!\n\n"); ?>
As you can see, the script makes extensive use of fputs to output data to STDOUT. This may seem merely like a trickier way of writing using print(), but it is much more flexible because one could use STDERR in place of STDOUT and redirect error output to a specific place.
The only part of the script which might confuse you at first is the call to fgets(). In the example, we read from STDIN in order to allow the user to enter up to 256 bytes of text. This input is then passed through trim() and then strtolower() before it is placed into the $sometext variable. The trim() function is used here because if users do not wish to enter the full 256 bytes of information, they can terminate their input by hitting Return. If their input is not sent through trim() first, their string is stored in $sometext with the extra \n (new line) intact.
The last line of the script outputs our user's string back to the screen, to complete the circle. Reading and writing data inside the console is quite straightforward, as the above example shows. Save the script as cli3.php and try editing it a little yourself in order to get a good grasp of how it works - be sure to try redirecting different parts of the output, as this is one of the best ways to take advantage of the STD* constants.
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Sending code direct to PHP >>
Previous chapter: Getting down and dirty
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.