TOC Previous Next
PHP as a command-line scripting language
Although PHP is mostly used for developing Web applications, it can also be used as a command-line scripting language as an alternative to CL. PHP has three command-line modes: interactive, one line at a time, and in scripts.
For details about using PHP as a command-line tool (not specifically for i5/OS), see the following Web address:
http://www.php.net/manual/en/features.commandline.php
PHP interactive mode
Interactive mode enables you to test bits of PHP code. To run PHP in interactive mode, type the following command in i5/OS PASE:
/usr/local/Zend/Core/bin/php -a
When PHP is in interactive mode, you can write PHP statements enclosed in <?php and ?> delimiters. For example:
<?php for ($i=0; $i<10; $i++) { print "Counting $i\n"; } ?>
Usually PHP output is buffered, so if you want to disable buffering, you can enter following command:
<?php while (@ob_end_clean()); ?>
To exit from PHP interactive mode, issue following command:
<?php exit ?>
Figure 4-3 illustrates an example of using PHP in interactive mode.
/QOpenSys/usr/bin/\-sh
Interactive mode enabled
> <?php while (@ob_end_clean()); ?>
> <?php for ($i=0;$i<10;$i\+\+) \{ print "Counting $i\n"; } ?>
Counting 0
Counting 1
Counting 2
Counting 3
Counting 4
Counting 5
Counting 6
Counting 7
Counting 8
Counting 9
===> <?php exit ?>
F3=Exit F6=Print F9=Retrieve F11=Truncate/Wrap
F13=Clear F17=Top F18=Bottom F21=CL command entry
Figure 4-3 Example of PHP interactive mode
PHP one-line scripts (popularly called oneliners)
To use PHP as a one-line script interpreter, you have to use the -r command line option. For example:
/usr/local/Zend/Core/bin/php -r 'for ($i=0;$i<10;$i++) { print "Counting $i\n"; }'
When this example is executed, the output will be similar to Figure 4-4.
/QOpenSys/usr/bin/\-sh
$
> /usr/local/Zend/Core/bin/php \-r 'for ($i=0;$i<10;$i\+\+) \{ print "Counting $i\n
"; }'
Counting 0
Counting 1
Counting 2
Counting 3
Counting 4
Counting 5
Counting 6
Counting 7
Counting 8
Counting 9
$
===>
F3=Exit F6=Print F9=Retrieve F11=Truncate/Wrap
F13=Clear F17=Top F18=Bottom F21=CL command entry
Figure 4-4 Output of PHP one line script
PHP and scripting
To run command-line scripts in PHP, create a file with PHP code as illustrated in Example 4-2, and later execute it as shown (using myfile.php as example):
/usr/local/Zend/Core/bin/php myfile.php
Example 4-2 PHP scripting example (myfile.php)
Output will be similar to that shown in Figure 4-5.
/QOpenSys/usr/bin/\-sh
kost myfile.php pero.php test.php testzend.php
$
> /usr/local/Zend/Core/bin/php myfile.php
Counting 0
Counting 1
Counting 2
Counting 3
Counting 4
Counting 5
Counting 6
Counting 7
Counting 8
Counting 9
$
===>
F3=Exit F6=Print F9=Retrieve F11=Truncate/Wrap
F13=Clear F17=Top F18=Bottom F21=CL command entry
Figure 4-5 PHP scripting output
To simply check the syntax of PHP file, you can do it by using the following -l command line option:
/usr/local/Zend/Core/bin/php -l myfile.php
If no syntax errors are found, the output will be similar to that shown in Figure 4-6.
/QOpenSys/usr/bin/\-sh
$
> cd /www/zendcore/htdocs/
$
> /usr/local/Zend/Core/bin/php \-l myfile.php
No syntax errors detected in myfile.php
$
===>
F3=Exit F6=Print F9=Retrieve F11=Truncate/Wrap
F13=Clear F17=Top F18=Bottom F21=CL command entry
Figure 4-6 PHP syntax check
Calling PHP from CL
It is possible to call PHP scripts from within CL applications. The following examples show how to invoke a PHP script that sends an e-mail using the built-in "mail" function.
Example 4-3 shows the PHP script that does the actual e-mail send operation. Of course you will want to replace the sender and recipient to actual values, and you must have SMTP configured correctly in your PHP configuration.
Example 4-3 PHP script: emailme.php
Example 4-4 shows the CL program (compiled as CALLPHP) that invokes the previous PHP script via PASE for i5/OS.
Example 4-4 CL program: CALLPHP
You can invoke the CALLPHP CL program the same way any other program under i5/OS:
CALL CALLPHP
Example 4-5 shows the results of calling the CL program.
Example 4-5 Result from invoking CALLPHP CL program
TOC Previous Next