PHP scripts are executed in one of two ways; through a web sever where the output is sent to a browser, or through the command-line interface. Main difference between the two methods is the format of new lines. Output to a browser requires the HTML line break <br /> while the output to a command-window requires \n. This page provides a basic introduction to PHP CLI (command line interface). OverviewUniform Server is portable, it does not make changes to your PC. As a consequence the standard command prompt (console window) cannot be used to run PHP CLI code. The path to php.exe along with other environment variables are undefined. To run PHP CLI code on Uniform Server use the Server Console. The Server Console button opens a command window (command prompt) with environment variables preset and paths configured for Uniform Server utilities including the PHP interpreter. Apart from cosmetics, it is identical to a standard command prompt. PHP code is run from the command prompt either directly by typing lines of code or indirectly using code saved in a script file. Command-windowRunning scripts via a command-line is performed using the CLI interpreter php.exe this program is located in folder C:\UniServerZ\core\php70 note UniController automatically tracks the installation and changes this path accordingly. In addition UniController sets several environment variables that are required for portability.
Note 1: You can clear the command-window by typing CLS PHP Command-line - run code directelyThe above demonstrates running PHP code directly using the CLI interpreter php.exe Example code repeated below: php.exe -n -r "print phpversion();" The interpreter program takes a number of parameters who’s function are follows:
Note: If your code contains quotes to avoid problems these must be escaped using a backslash for example: php -r "print \"test\n\n\";" //-Print test and two new lines \n Code examplesThe following provide examples that you can cut and past into the command-window:
With one exception all above examples are forced not to use a configuration file by using the parameter –n. PHP Command-line - run scriptsEntering code directly is ideal for short scripts, however for scripts containing several lines of code it is preferable to save these to a file and run this file from the Server Console. The following example shows how to run a PHP script with and without a PHP configuration file. If using a configuration file you have the option to use either the default or your own specific file.
CLI configuration fileYou can choose to run the PHP interpretor with or without a configuration file. When running with a configuration file you have two options, either use the default configuration file or use your own configuration file. These are defined by parameters as follows:
Default Configuration file: Default configuration file php-cli.ini has been configured specifically for Uniform Server. You can add to this file, however make sure you do not make changes to the existing configuration. Note: The default configuration file used is dependent on the PHP version selected (see Select PHP version) and will be one of the following:
User Configuration file: To avoid issues at a later date it is probably wise to use your own configuration file for example my_cli.ini this can be a name of your own choice. Create this file by making a copy of php-cli.ini in the same folder and rename it my_cli.ini make changes to this file as required. To force the CLI interpreter php.exe to use a different configuration file use the parameter –c followed by the full or relative path to the configuration file my_cli.ini for example: php -c K:\UniServerZ\core\php70\my_cli.ini php -c core\php70\my_cli.ini A problem with the above paths, they are not portable or PHP switchable. Moving folder UniServerZ to a different location the first will fail because of the path change. Switching the PHP version for example to php53 both fail because of the folder change and php53 selection. A solution is to use the environment variable PHP_SELECT as follows: php -c core\%PHP_SELECT%\my_cli.ini To confirm the file is being picked-up correctly by PHP type the following K:\UniServerZ>php -c core\%PHP_SELECT%\my_cli.ini -r "print(php_ini_loaded_file ());" K:\UniServerZ\core\php54\my_cli.ini K:\UniServerZ> Summary: php -c core\php53\my_cli.ini //Fixed PHP php -c core\php54\my_cli.ini //Fixed PHP php -c core\php55\my_cli.ini //Fixed PHP php -c core\php56\my_cli.ini //Fixed PHP php -c core\php70\my_cli.ini //Fixed PHP php -c core\%PHP_SELECT%\my_cli.ini //Switchable PHP Note: An alternative, although not PHP switchable, is to locate the configuration file in the root folder UniServerZ. Give the configuration file an appropriate name for example my_cli_53.ini, my_cli_54.ini, my_cli_55.ini, my_cli_56.ini or my_cli_70.ini This reduces amount of typing and the files are fully portable, use the one that matches the PHP selected. php -c my_cli_53.ini php -c my_cli_54.ini php -c my_cli_55.ini php -c my_cli_56.ini php -c my_cli_70.ini Additional informationThis section is included for completeness. Information provided above is for a preconfigured server however for experimentation there is no reason to use a fully blown server. The following describes a minimum PHP installation which may be more suitable: Minimum requirements The above combination is fully portable with a footprint of 53MB if you wish this can be pruned down see section Reduce footprint. Reduce footprint A) If you are not using the Internationalization extension (php_intl.dll) you can delete the following: K:\UniServerZ\core\php54\extensions\php_intl.dll K:\UniServerZ\core\php54\icudt53.dll K:\UniServerZ\core\php54\icuin53.dll K:\UniServerZ\core\php54\icuio53.dll K:\UniServerZ\core\php54\icule53.dll K:\UniServerZ\core\php54\iculx53.dll K:\UniServerZ\core\php54\icutest53.dll K:\UniServerZ\core\php54\icutu53.dll K:\UniServerZ\core\php54\icuuc53.dll The above achieves a significant reduction of 24MB B) If you are not using the PHP mail function disable this in the configuration files php-cli.ini and my_cli.ini by commenting (add ; to beginning of line) the line as shown below: ;sendmail_path = "${US_ROOTF}/core/msmtp/msmtp.exe --file=${US_ROOTF}/core/msmtp/msmtprc.ini -t" Delete folder K:\UniServerZ\core\msmtp saves 2.89MB C) You can further reduce the footprint by deleting extensions and any corresponding dependency this is left to your discretion as to what is deleted. However check the following dependency information before deleting an extension and or its corresponding dependency dll:
Emulate UniController command consoleAlthough the UniController command console is ideal for running PHP scripts it has one disadvantage, to open the command console requires you to run UniController first. An alternative is to emulate UniController using a batch file this is covered on page Batch Files- Emulate Server Console. Passing parametersYou can pass parameters between scripts such as a batch file and PHP for details see page Batch Files- Passing parameters. --oOo--
|