메뉴 건너뛰기

enjoyTools.net

리눅스에서 php -r

2018.07.25 08:54

꿈돌이 조회 수:1021

http://php.net/manual/en/features.commandline.options.php

따옴표 문제가 있다고 한다.

 

-r --run

Allows execution of PHP included directly on the command line. The PHP start and end tags (<?php and ?>) are not needed and will cause a parse error if present.

Note:

Care must be taken when using this form of PHP not to collide with command line variable substitution done by the shell.

Example #2 Getting a syntax error when using double quotes

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

Parse error: syntax error, unexpected '=' in Command line code on line 1

The problem here is that sh/bash performs variable substitution even when using double quotes ". Since the variable $foo is unlikely to be defined, it expands to nothing which results in the code passed to PHP for execution actually reading:

$ php -r " = get_defined_constants();"

The correct way would be to use single quotes '. Variables in single-quoted strings are not expanded by sh/bash.

Example #3 Using single quotes to prevent the shell's variable substitution

$ 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"]=>
  [...]

If using a shell other than sh/bash, further issues might be experienced - if appropriate, a bug report should be opened at » http://bugs.php.net/. It is still easy to run into trouble when trying to use variables (shell or PHP) in commnad-line code, or using backslashes for escaping, so take great care when doing so. You have been warned!

Note:

-r is available in the CLI SAPI, but not in the CGI SAPI.

Note:

This option is only intended for very basic code, so some configuration directives (such as auto_prepend_file and auto_append_file) are ignored in this mode.

 

 

번호 제목 글쓴이 날짜 조회 수
공지 툴 북마크 꿈돌이 2021.02.11 80940
37 윈도우10 앱 및 기능 에서 삭제 안되는 프로그램을 목록에서 강제로 제거하기 꿈돌이 2019.05.02 12554
36 mysql_error() equivalent is mssql_get_last_message() 꿈돌이 2019.03.11 355
35 certbot 수동 갱신 꿈돌이 2019.01.27 201
34 GnuWin32 패키지 목록 페이지 꿈돌이 2018.12.14 2650
33 윈도우 10 260자 초과하는 파일경로 허용 꿈돌이 2018.11.03 2358
32 라즈베리파이 USB메모리 자동 마운트 꿈돌이 2018.10.30 699
31 윈도우 cmd에서 랜카드 dhcp on/off 꿈돌이 2018.09.29 457
» 리눅스에서 php -r 꿈돌이 2018.07.25 1021
29 우분투 USB 리셋 - MS 무선마우스 휠 문제 꿈돌이 2018.06.18 218
28 우분투 키보드 입력 속도 조정 꿈돌이 2018.06.18 69058
27 디버그모드에서 안드로이드 instant run file 꿈돌이 2018.04.20 200
26 라즈베리파이 메모 file 꿈돌이 2018.02.13 410
25 라즈베리파이 전용 7인치 터치LCD 제어 꿈돌이 2018.01.30 2051
24 안드로이드 스마트폰으로 웹서버 만들기 꿈돌이 2018.01.04 1159
23 LZ4 압축하기, 해제하기 간단 예제 file 꿈돌이 2017.04.18 25110
22 golang Powershell에서 현재 디렉터리를 gopath로 지정 꿈돌이 2017.04.03 350
21 Git (MinGW) 인증서 설정 꿈돌이 2017.03.22 285
20 Git 사용자 설정 꿈돌이 2017.03.18 120
19 Git .gitignore 꿈돌이 2017.03.18 781
18 node.js 전역 모듈 제거 꿈돌이 2017.03.16 3545