Zero logo

PHP CLI

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).

Minimum requirements

In order to run PHP CLI scripts two Uniform Server ZeroXI modules are required. A controller ZeroXI_controller_1_1_8.exe and one of the PHP modules for example ZeroXI_php_5_4_35.exe

The above combination is fully portable with a footprint of 53MB if you wish this can be pruned down see section Reduce footprint.

Command-window

Running scripts via a command-line is performed using the CLI interpreter php.exe this program is located in folder C:\Zero_11\UniServerZ\core\php54 note UniController automatically tracks the installation and changes this path accordingly. In addition UniController sets several environment variables that are required for portability.

Check functionality:
  • Start UniController by double clicking on file UniController.exe
  • Start a command-window by clicking on the Server Console button.

Note: You can open more than one command-window.

Type the following into the command-window: Displays PHP version number

php.exe -n -r "print phpversion();"
  PHP CLI intro

Note 1: You can clear the command-window by typing CLS
Note 2: For help type php --help.

PHP Command-line - run code directely

The above demonstrates running PHP code directly using the CLI interpreter php.exe

php.exe -n -r "print phpversion();"

The interpreter program takes a number of parameters who’s function are follows:

php.exe CLI interpreter program php.exe If a file extension is not provided Windows will assume .exe hence you can use just php
-n Do not use a PHP configuration file
-r Run the following PHP code directely
"print phpversion();"   PHP code to run. Windows requires the code to be enclosed quotes.

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 examples

The following provide examples that you can cut and past into the command-window:

Examples 1:
php -n -r "echo 'test1';"             //-Display literal
php -n -r "echo \"test2\";"           //-Escape the quotes
php -n -r "$a=5; $b=6; print \"$a\n\"; print \"$b\n\"; print \"$a+$b\";"
php -n -r "$foo = get_defined_constants(); var_dump($foo);"
  Examples 2:
php -n -r "print phpversion();"          //-- Display version
php -n -r "phpinfo();"                   //-- Display information
php -n -r "phpinfo();"|more              //-- Display a page
php -r "print(php_ini_loaded_file ());"  //-- Display path to ini

CLI configuration file

With one exception all above examples are forced not to use a configuration file by using the parameter –n. The last example does not use this parameter and prints the default configuration file loaded.

Configuration file displayed:
K:\UniServerZ\core\php54\php-cli.ini
 
K:\UniServerZ>php -r "print(php_ini_loaded_file ());"
K:\UniServerZ\core\php54\php-cli.ini
K:\UniServerZ>

The current configuration of this file is specifically for Uniform Server you can use this file however do not make changes to the existing configuration.

Note: The default configuration file used is dependent on the PHP version selected and will be one of the following:

  • K:\UniServerZ\core\php53\php-cli.ini
  • K:\UniServerZ\core\php54\php-cli.ini
  • K:\UniServerZ\core\php55\php-cli.ini

User Configuration

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\php54\my_cli.ini
php -c core\php54\my_cli.ini

A problem with the above paths they are not portable or PHP switch able. Moving folder UniServerZ to a different location the first will fail because of the path change. Switching the PHP version for example to php5.3.7 both fail because of the folder change php53. 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 -r "print(php_ini_loaded_file ());" after parameter –c for example:

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:
Use one of the following lines to load your configuration file:

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 switch-able is to locate the configuration file in the root folder UniServerZ with 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.iniThis 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

Running PHP scripts

Creare script:

Create a test script shown on the right and save to
root folder UniServerZ with name test1.php

  test1.php
<?php
 print "Example script\n";
 print "Configuration file used:\n";
 print(php_ini_loaded_file () . "\n");
 print "End example script.\n";
?> 
Run script:

Run the script by typing the following into a command-window:

php -n test1.php

Note: No configuration loaded because we used -n.

  Result1
K:\UniServerZ>php test1.php
Example script
Configuration file used:

End example script.

K:\UniServerZ>
Run script:

Run the script by typing the following into a command-window:

php test1.php

Note: Default configuration is loaded.

  Result2
K:\UniServerZ>php test1.php
Example script
Configuration file used:
K:\UniServerZ\core\php54\php-cli.ini
End example script.

K:\UniServerZ>
Run script:

Run the script by typing the following into a command-window:

php -c core\php54\my_cli.ini test1.php

Note: Configuration file my_cli.ini is loaded.

  Result3

K:\UniServerZ>php -c core\php54\my_cli.ini test1.php
Example script
Configuration file used:
K:\UniServerZ\core\php54\my_cli.ini
End example script.

K:\UniServerZ>

Reduce footprint

Although the footprint is 52MB it contains a full version of PHP and some additional utilities. You can reduce this footprint as follows:

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:

Dependency information:
Module: php_couchbase.dll
===========================
	libcouchbase.dll

Module: php_curl.dll
===========================
	libeay32.dll
	ssleay32.dll

Module: php_excel.dll
===========================
	libxl.dll

Module: php_ffmpeg.dll
===========================
	avcodec-52.dll
	avformat-52.dll
	avutil-50.dll
	swscale-0.dll

Module: php_imagick.dll
===========================
	core_rl_wand_.dll
	core_rl_magick_.dll

Module: php_intl.dll
===========================
	icuuc51.dll
	icuin51.dll


Dependency information:
Module: php_ldap.dll
===========================
	libsasl.dll
	ssleay32.dll
	libeay32.dll

Module: php_oauth.dll
===========================
	libeay32.dll
	ssleay32.dll

Module: php_openssl.dll
===========================
	ssleay32.dll
	libeay32.dll

Module: php_pdflib.dll
===========================
	pdflib.dll

Module: php_pgsql.dll
===========================
	libpq.dll

Module: php_pspell.dll
===========================
	aspell-15.dll

Module: php_pthreads.dll
===========================
	pthreadvc2.dll
Dependency information:

Module: php_snmp.dll
===========================
	libeay32.dll

Module: php_sockets.dll
===========================
	iphlpapi.dll

Module: php_sqlsrv.dll
===========================
	msvcp90.dll

Module: php_ssh2.dll
===========================
	libeay32.dll

Module: php_stomp.dll
===========================
	ssleay32.dll

Module: php_sybase_ct.dll
===========================
	libsybcs.dll
	libsybct.dll

Module: php_couchdb.dll
===========================
	libeay32.dll
	ssleay32.dll

Dependency information:
Module: php_enchant.dll
===========================
	libenchant.dll

Module: php_win32ps.dll
===========================
	psapi.dll

Module: php_crypto.dll
===========================
	libeay32.dll

Module: php_http.dll
===========================
	libeay32.dll
	ssleay32.dll

Module: php_pdo_pgsql.dll
===========================
	libpq.dll

Module: php_pdo_sqlsrv.dll
===========================
	msvcp90.dll

Module: php_solr.dll
===========================
	libeay32.dll
	ssleay32.dll



--oOo--