PHP CLI: Introduction

From The Uniform Server Wiki
Revision as of 09:48, 15 August 2009 by Ric (talk | contribs) (New page: {{Uc nav PHP CLI}} '''''Introduction and basics''''' PHP CLI is a very powerful tool and worth revisiting. This series provides real working examples you can run using Uniform Server. If ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

 

MPG UniCenter

UniServer 5.0-Nano
PHP CLI.

Introduction and basics

PHP CLI is a very powerful tool and worth revisiting. This series provides real working examples you can run using Uniform Server. If you wish to run the examples download a copy of UniServer 5.0-Nano and extract to any folder.

The server is just a convenient method of obtaining the PHP binaries along with some useful UniServer components. Strictly speaking there is no need to run the servers CLI scripts are independent. That said a server comes in handy when testing some scripts.

No Windows

Generally speaking a user interacts with a CLI script using a command prompt and not a GUI Window. It is possible and not unreasonable to interface to a CGI script using a web page. Then again there are no real restrictions if you want to design a Windows GUI interface the point I am trying to make; there are no hard and fast boundaries they tend to be grey and fuzzy. The code snippets in this series are run via a batch file, it’s a convenient way to start a command prompt and interface to a CLI script.

Top

Components

Interestingly PHP is inherently portable by this I mean it does not depend on any registry entries for its operation. You can pop the core component on a USB memory stick and run it from there. PHP is designed to integrate tightly with a server and serve web pages efficiently. A server passes a PHP script to the core component for processing and servers the result to a users browser.

To run PHP in CLI mode the server is replaced by a small program (CLI interface) that passes a PHP script to the core component for processing and directs the result of this processing to the calling device or program.

Hence there are two components required to run PHP in CLI mode:

  1. Core PHP php5ts.dll
  2. CLI Interface php.exe
  3. CLI Interface php-win.exe Identical to the above but runs in the background hidden covered later.

These can be found in folder UniServer\usr\local\php copy php5ts.dll and php.exe to a new folder named cli_test

Note: They can be run from their current location covered latter.

The only reason for moving the above to a separate folder is to remove any unnecessary clutter, makes it easier to see whats going on.

Top

Hello World

Hello World scripts are very basic and indented to confirm an instillation works correctly, these two scripts are provided for that very reason:

We need a script to pass to the main component for processing:

Still in folder cli_test create a new text file named test_1.php and add the following content:

test_1.php  
<?php
echo "Hello World\n";
exit(0); // Script ran OK
?>

I would recommend you always use full tags <?php ?> and not the short form <? ?> saves grief in the future when you want to move scripts especially web pages.

Always provide an exit code 0=OK any other number indicates fail.

One final script is required a batch file to run the command line interface program. Create a new text file named Run.bat with the following content:

Run.bat  
TITLE CLI TEST BAT
COLOR B0
@echo off
cls
echo.
php.exe test_1.php
echo.
pause
?>
  • TITLE - displays text that follows it in the command window title bar.
  • COLOR - sets the background colour of the command window
  • @echo off - prevents batch file commands being displayed
  • cls - clears any content displayed in the command window
  • echo. - display a blank line
  • php.exe test_1.php - Runs the command line interface program php.exe it is passed one parameter test_1.php
  • echo. - display a blank line
  • pause - prevents the command window closing, allows you to see whats displayed.

Top

Run Script

Run the script by double clicking on Run.bat This runs the command line interface php.exe it takes one parameter, the full path and name of our script test_1.php, passes this information onto the main component (php5ts.dll) for processing.

Our script echos a new line character, text "Hello World" followed by a newline character. The command line interface php.exe receives this and passes it back to the command-window where it is displayed.

Note 1:

This line php.exe test_1.php actually reads as follows full_path_to\php.exe full_path_to\test_1.php if a full path is not provided programs look for the required files in the current folder.

Note 2:

To create a new line, Web pages use <b /> however CLI scripts require \n minor difference to see this in action add more \n to the test script just after the first one.

Top

Portable

Copy folder cli_test to a USB memory stick, plug it into another PC and run the script.

PHP is fully portable and independent, to unleash its power you do not require a full instalation of PHP .

Top

Summary

The remainder of this series uses the above concepts however examples provided use the existing PHP core and CLI interface contained within UniServer’s architecture.

A problem with the simple hello world example is that it isolates you from real world implementations. Essentially masks the requirement for full paths and interaction with other elements this scenario is covered on the next page

Top


MPG (Ric)