PHP 4

[ PHP ]

(!!! Teleportom!!!)
: fopen, msql_num_fields, socket_write ...


24. PHP

4.3, SAPI- (Server Application Programming Interface) CLI, Command Line Interface. , SAPI- /shell ( ) . CLI SAPI SAPI, .

CLI SAPI PHP 4.2.0, , --enable-cli ./configure. PHP 4.3.0, CLI SAPI php ( php.exe Windows).

CLI SAPI SAPI:

  • CGI SAPI, /headers .

    CGI SAPI HTTP-, CLI SAPI .

  • php.ini, CLI SAPI, :

24-1. php.ini-
CLI SAPI-
html_errorsFALSE , HTML-, FALSE.
implicit_flushTRUE , print(), echo() , - . , .
max_execution_time0 (unlimited) - PHP , . , web, , .
register_argc_argv TRUE PHP $argc ( , ) $argv ( ) CLI SAPI.

: php.ini ( ). , . ( , , register_argc_argv).

  • , :

    24-2. CLI-
    STDIN stdin.
    $stdin = fopen('php://stdin', 'r');
    STDOUT stdout.
    $stdout = fopen('php://stdout', 'w');
    STDERR stdout.
    $stderr = fopen('php://stderr', 'w');

    , , , stderr, :

    php -r 'fwrite(STDERR, "stderr\n");'

    , .

  • CLI SAPI !

    , CGI SAPI:

    <?php
        /*    */
        echo getcwd(), "\n";
    ?>

    CGI-, :

    $ pwd
    /tmp
    
    $ php-cgi -f another_directory/test.php
    /tmp/another_directory

    , PHP .

    CLI SAPI :

    $ pwd
    /tmp
    
    $ php -f another_directory/test.php
    /tmp

    PHP.

    : CGI SAPI CLI SAPI -C .

PHP PHP -h:

Usage/: php [options] [-f] <file> [args...]
       php [options] -r <code> [args...]
       php [options] [-- args...]
  -s                    .
  -w                     .
  -f <file>         <file>.
  -v                .
  -c <path>|<file>   php.ini   .
  -a                .
  -d foo[=bar]      INI- foo   'bar'.
  -e                   /profiler'.
  -z <file>         <file> Zend-.
  -l                  (lint).
  -m                 .
  -i               PHP-.
  -r <code>         PHP <code>     <?..?>
  -h                help.

  args...          ,  .  -- args 
                       -     stdin

CLI SAPI PHP-, :

  1. PHP .

    php my_script.php
    
    php -f my_script.php

    (/ -f) my_script.php. ; PHP .php, .

  2. PHP .

    php -r 'print_r(get_defined_constants());'

    .

    : , ! -r . .

  3. PHP- (stdin).

    PHP- , () :

    $ some_application | some_filter | php | sort -u >final_output.txt

.

, PHP, PHP . ( ).
, , $argv. ( - , PHP- -r).
$argc, $argv ( , ).

, , (-), . , - , , PHP , . , --. PHP, / .

#     ,    PHP
$ php -r 'var_dump($argv);' -h
Usage: php [options] [-f] <file> [args...]
[...]

#  '-h'     ,    PHP  
$ php -r 'var_dump($argv);' -- -h
array(2) {
  [0]=>
  string(1) "-"
  [1]=>
  string(2) "-h"
}

, PHP . , #!/usr/bin/php, PHP-, PHP, . perl:

#!/usr/bin/php
<?php
    var_dump($argv);
?>

, test , :

$ chmod 755 test
$ ./test -h -- foo
array(4) {
  [0]=>
  string(6) "./test"
  [1]=>
  string(2) "-h"
  [2]=>
  string(2) "--"
  [3]=>
  string(3) "foo"
}

, , -.

24-3.
-s

.

, HTML- . , <code> [...] </code> HTML- HTML-header.

: -r.

-w

.

: -r.

-f

. . .

-v

PHP, PHP SAPI Zend- , :
$ php -v
PHP 4.3.0-dev (cli), Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.2.1, Copyright (c) 1998-2002 Zend Technologies

-c

php.ini, INI- ( php.ini), :
$ php -c /custom/directory/ my_script.php

$ php -c /custom/directory/custom-file.ini my_script.php

-a

PHP .

-d

, php.ini. :
-d configuration_directive[=value]

:
#   value      "1"
$ php -d max_execution_time -r '
$foo = ini_get("max_execution_time"); var_dump($foo);'
string(1) "1"

#    value      ""
php -d max_execution_time= -r '
$foo = ini_get("max_execution_time"); var_dump($foo);'string(0) ""

#      , 
#    '='
$  php -d max_execution_time=20 -r '
$foo = ini_get("max_execution_time"); var_dump($foo);'
string(2) "20"
$  php -d max_execution_time=doesntmakesense -r '
$foo = ini_get("max_execution_time"); var_dump($foo);'
string(15) "doesntmakesense"

-e

debugger/profiler.

-z

Zend-. , PHP ( /etc/ld.so.conf Linux-). filename . filename PHP - .

-l

PHP-. - No syntax errors detected in <filename> , return- 0. - Errors parsing <filename> , return- 255.

( ). -f, .

: -r.

-m

, PHP ( ) PHP Zend:
$ php -m
[PHP Modules]
xml
tokenizer
standard
session
posix
pcre
overload
mysql
mbstring
ctype

[Zend Modules]

-i phpinfo() . PHP , php -i , . , HTML , , .
-r

PHP . PHP (<?php ?>) .

: PHP, , .

, :

$ php -r "$foo = get_defined_constants();"
Command line code(1) : Parse error - parse error, unexpected '='

, sh/bash ". $foo , , , PHP , :

$ php -r " = get_defined_constants();"

'. , , sh/bash.

$ php -r '$foo = get_defined_constants(); var_dump($foo);'
array(370) {
  ["E_ERROR"]=>
  int(1)
  ["E_WARNING"]=>
  int(2)
  ["E_PARSE"]=>
  int(4)
  ["E_NOTICE"]=>
  int(8)
  ["E_CORE_ERROR"]=>
  [...]

/shell , sh/bash, . bug report mail [email protected] (/) /escaping.
...

-h .

PHP PHP- web-.
Unix, PHP- , , .
Windows php.exe .php batch- (.bat) PHP. , Unix, Windows, . - .

24-1. , (script.php)
#!/usr/bin/php
<?php

if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>

This is a command line PHP script with one option.

  Usage:
  <?php echo $argv[0]; ?> <option>

  <option> can be some word you would like
  to print out. With the --help, -help, -h,
  or -? options, you can get this help.

<?php
} else {
    echo $argv[1];
}
?>

, PHP. CLI-, HTTP-/header. , - : $argc $argv. - 1 ( ). - , ($argv[0]).

, . , --help, -help, -h -?, help-, . - , (echo).

Unix, executable script.php echothis script.php -h. Windows batch- :

24-2. /Batch- PHP- (script.bat)
@c:\php\php.exe script.php %1 %2 %3 %4

, script.php php.exe c:\php\php.exe, batch- : script.bat echothis script.bat -h.

. Readline , - .


java script

: 3119 .

: - |  -