Virtual Private Servers - now 40% off
click here for details

^ click above ^
07.16.03


By Jayesh Jain

I know that you all want to get rid of Perl Scripts because of their complexity and the fact that Perl is not an easy language to learn. With the introduction of PHP version 4.2, PHP has started supporting a new SAPI (Server Application Programming Interface) called CLI (Command Line Interface). This facility was introduced to help developers create small shell application (scripts) with PHP, meaning that you can kiss Perl goodbye forever!

The CLI SAPI was released for the first time with PHP 4.2.0, but was still experimental back then and had to be explicitly enabled with --enable-cli when running ./configure. With PHP 4.3.0, the CLI SAPI will no longer be experimental and therefore will always be built and installed as the PHP (called php.exe on Windows) binary.

In this tutorial I will show you how to use PHP's CLI feature to interpret shell scripts. I will assume that you have a fair understanding of PHP and that PHP is installed and working properly on your computer. You can have PHP installed on Linux or Windows (the examples in this tutorial are demonstrated with PHP installed under Windows but they should work the same on a Linux machine).

What are PHP Shell Scripts?

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:
  1. Shell script can take input from a user or file and output them on screen

  2. Useful to create your own commands/applications

  3. Don't have to reinvent the wheel

  4. 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 (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
[Note] 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 [End Note]

There are three streams available in the PHP CLI, which are:
  1. stdin ('php://stdin')

  2. stdout ('php://stdout')

  3. stderr ('php://stderr')
This following example will display "Hello World" in the output window using the output stream:
<? 
$stdout = fopen('php://stdout', 'w'); 
fwrite($stdout,"Hello World"); 
fclose($stdout); 
?>
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.

Click Here to Read the Full Article

About the Author:
Jayesh Jain is working as applications consultant for a health company in Auckland, New Zealand. He has several years of n-Tier development experience and is currently working with Visual Basic.NET to develop interactive client solutions. He has a passion for Web development and in the spare time he likes to write articles. Contact him at: jainjayesh74@yahoo.com

Read this newsletter at: http://www.sysadminnews.com/2003/0716.html

 

 

 

 

 

 

 

 

Get in-touch with industry experts and leaders
Make Freelance contacts
Post your site for review by expert and peers
Ask SEO, Marketing, Development and Design questions
Join a community of your peers in all fields, ready to help
Username:
Email: *
Password:
Confirm Password:
* Information will not be shared with 3rd parties

Already Registered?
Click Here to Login.

-- SysAdminNews is an iEntry, Inc. publication --
2003 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal