After a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and discuss the PHP command line with you. PHP Command Line Interface (CLI) Server Application Programming Interface (SAPI) was released starting with PHP V4.2.0 for experimental purposes. As of V4.3.0, it is fully supported and enabled by default.
<ol class="dp-xml"><li class="alt"><span><span>shell_exec() </span></span></li></ol>
The shell_exec() command line is really just a variation of the backtick (`) operator. If you've ever written a shell or Perl script, you know that you can capture the output of other commands inside the backtick operator. For example, Listing 1 shows how to use backticks to get the word count for each text (.txt) in the current directory.
PHP Command Line Overview
Listing 1. Counting word count using backticks
<ol class="dp-xml"> <li class="alt"><span><span>#! /bin/sh </span></span></li> <li class=""> <span></span><span class="attribute"><font color="#ff0000">number_of_words</font></span><span>=`wc -w *.txt` </span> </li> <li class="alt"><span>echo $number_of_words </span></li> <li class=""><span> </span></li> <li class="alt"><span>#result would be something like: </span></li> <li class=""><span>#165 readme.txt 388 results.txt 588 summary.txt </span></li> <li class="alt"><span>#and so on.... </span></li> </ol>
In your In a PHP script, you can run this simple command in shell_exec(), as shown in Listing 2, and get the results you want. It is assumed here that there are some text files in the same directory.
Listing 2. Running the same command in shell_exec()
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>results</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>shell_exec</FONT></SPAN><SPAN>('wc -w *.txt'); </SPAN></SPAN><LI class=alt><SPAN>echo $results; </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
Note that just using the trailing apostrophe operator will give you the same result, as follows shown.
Listing 3. Using only the trailing apostrophe operator
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>results</FONT></SPAN><SPAN> = `wc -w *.txt`; </SPAN></SPAN><LI class=alt><SPAN>echo $results; </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
Listing 4. A simpler approach
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>echo `wc -w *.txt`; </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
It’s important to know that a lot can be accomplished through the UNIX command line and shell scripts. For example, you can use pipes to connect commands. You can even create a shell script in it using operators and just call the shell script (with or without parameters as needed). For example, if you only want to count the words in the first 5 text files in that directory, you can use a pipe (|) to connect the wc and head commands. Alternatively, you can place the output inside a pre tag to render it more beautifully in a web browser, as shown below.
Listing 5. More complex shell commands
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>results</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>shell_exec</FONT></SPAN><SPAN>('wc -w *.txt | head -5'); </SPAN></SPAN><LI class=alt><SPAN>echo "</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>pre</SPAN><SPAN class=tag>></span></font></strong><span>".$results . "</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>pre</SPAN><SPAN class=tag>></span></font></strong><span>"; </span></span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
Later in this article, you will learn how to pass arguments to these scripts using PHP. Now you can think of this as a way to run a shell command, but remember that you can only see standard output. If an error occurs with a command or script, you won't see the standard error (stderr) unless you add it to stdout via a pipe.