다른 SAPI와의 차이점

다른 SAPI와 비교한 CLI SAPI의 현저한 차이점:

  • CLI SAPI와 달리 헤더는 출력에 기록되지 않습니다.

    CLI SAPI는 HTTP 헤더를 억제하는 방법을 제공하지만 CLI SAPI에서 이를 활성화하는 동등한 스위치는 없습니다.

    CLI는 기본적으로 자동 모드에서 시작되지만 -q--no-header 스위치는 이전 CGI 스크립트를 사용할 수 있도록 호환성을 위해 유지됩니다.

    작업 디렉토리를 스크립트의 디렉토리로 변경하지 않습니다. (-C--no-chdir 스위치는 호환성을 위해 유지됨)

    일반 텍스트 오류 메시지(HTML 형식 없음).

  • 셸 환경에서 의미가 없기 때문에 CLI SAPI에 의해 재정의되는 특정 php.ini 지시문이 있습니다.

    재정의된 php.ini 지시문

    Directive CLI SAPI default value Comment
    html_errors false Defaults to false, as it can be quite hard to read error messages in the shell environment when they are cluttered up with uninterpreted HTML tags.
    implicit_flush true In a shell environment, it is usually desirable for output, such as from print, echo and friends, to be displayed immediately, and not held in a buffer. Nonetheless, it is still possible to use output buffering to defer or manipulate standard output.
    max_execution_time 0 (unlimited) PHP in a shell environment tends to be used for a much more diverse range of purposes than typical Web-based scripts, and as these can be very long-running, the maximum execution time is set to unlimited.
    register_argc_argv true

    Setting this to true means that scripts executed via the CLI SAPI always have access to argc (number of arguments passed to the application) and argv (array of the actual arguments).

    The PHP variables $argc and $argv are automatically set to the appropriate values when using the CLI SAPI. These values can also be found in the $_SERVER array, for example: $_SERVER['argv'].

    output_buffering false

    Although the php.ini setting is hardcoded to false, the Output buffering functions are available.

    max_input_time false

    The PHP CLI does not support GET, POST or file uploads.

    메모:

    이러한 지시문은 구성 파일 php.ini의 다른 값이나 사용자 지정 값(지정된 경우)으로 초기화할 수 없습니다. 이 제한은 모든 구성 파일이 구문 분석된 후에 값이 적용되기 때문입니다. 그러나 해당 값은 런타임 중에 변경될 수 있습니다(이는 register_argc_argv와 같이 모든 항목에 대해 합리적이지는 않지만).

    메모:

    명령줄 스크립트에 대해 ignore_user_abort를 설정하는 것이 좋습니다. 자세한 내용은 ignore_user_abort()를 참조하십시오.

  • 셸 환경에서 작업을 쉽게 하기 위해 I/O 스트림에 대해 여러 상수가 정의됩니다.
  • CLI SAPI는 현재 디렉토리를 실행된 스크립트의 디렉토리로 변경하지 않습니다.

    예제 #1 CGI SAPI와의 차이점을 보여주는 예:

                          
    <?php
    // Our simple test application named test.php
    echo getcwd(), "\n";
    ?>
                          
                        

    CGI 버전을 사용할 때 출력은 다음과 같습니다.

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

    이것은 PHP가 현재 디렉토리를 실행된 스크립트 중 하나로 변경한다는 것을 분명히 보여줍니다.

    CLI SAPI를 사용하면 다음을 얻을 수 있습니다.

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

    이것은 PHP에서 쉘 도구를 작성할 때 더 큰 유연성을 허용합니다.

    메모: CGI SAPI는 명령줄에서 실행할 때 -C 스위치를 통해 이 CLI SAPI 동작을 지원합니다.