An Alternative to Perl: Shell Scripting With PHP - What are PHP Shell Scripts? (Page 2 of 4 )
Normally shells are interactive, but not always. This means that the shell accepts commands from you through your keyboard and executes them. But instead of issuing command one by one, we can store this sequence of commands in a text file and tell PHP to execute this text file instead of entering the commands manually. This is known as PHP shell scripting.
PHP shell scripts are just like batch files in MS-DOS, but they have more power than the MS-DOS batch file, thanks to PHP.
Why Write Shell Scripts? Here are some reasons why you may want to consider writing PHP shell scripts:
Shell script can take input from a user or file and output them on screen
Useful to create your own commands/applications
Don't have to reinvent the wheel
Can be used to automate some day to day tasks, such as backups
Getting Started Lets start with a small script to display the typical "Hello World" text. Create a text file called world.php. Enter the following code into world.php and save it in your PHP directory:
<? echo "Hello World"; ?>
Open your command prompt change into the folder where PHP is installed and run the following command:
php world.php
If you're surprised to see the output at the command prompt instead of in a web browser, then welcome to the other dimension of PHP! You may have noticed that the following header is also included in the output (PHP does this be default, which also tells you the PHP version):
X-Powered-By: PHP/4.2.3 Content-type: text/html
To suppress this HTTP header, we could run PHP with the following command line parameter:
php -q world.php
Lets look at few of the command line options available with the PHP interpreter:
-q (Quiet mode. Suppress HTTP Header output)
-w (Display source with stripped comments and white space)
-v (Version number)
-c <path> (With this option one can either specify a directory where to look for php.ini or you can specify a custom INI file directly (which does not need to be named php.ini)
-d (This option allows you to set a custom value for any of the configuration directives allowed in php.ini. The syntax is: -d configuration_directive[=value])
-l (This option checks the syntax in the source file)
-i (This command line option calls phpinfo() and prints out the results)
Using Streams in Scripts You can redirect the output from any script to a file, just like this:
php world.php > outputfile
For all you Linux fans, you can also redirect the script output to another command by using the | (pipe operator) e.g. : php world.php | sort.
There are three streams available in the PHP CLI, which are:
stdin ('php://stdin')
stdout ('php://stdout')
stderr ('php://stderr')
This following example will display "Hello World" in the output window using the output stream:
This example will demonstrate how to use an input stream. It will accept input from the user and wait until the user presses the enter key and then it will display the text entered:
<? $stdin = fopen('php://stdin', 'r'); echo "Please Enter your Name :"; $mystr = fgets($stdin,100); echo "Your Name Is :\n"; echo $mystr; fclose($stdin); ?>
This following example shows you how to output text to an error stream:
<? $stderr = fopen('php://stderr', 'w'); fwrite($stderr,"There was an Error"); fclose($stderr); ?>
OK, before we move ahead, there are a few things that you should know. The output to the error stream is always sent to the error device (normally screen) and is not sent to file or another command when redirecting the output. Always make sure you close this stream once you are done with it. Please refer to the PHP manual if you need more information on the fopen, fwrite, fgets and fclose functions.