메뉴 건너뛰기

enjoyTools.net

리눅스에서 php -r

2018.07.25 08:54

꿈돌이 조회 수:1015

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 80864
56 3d 모델 사이트 꿈돌이 2020.03.02 1292
55 svm_c_ex.cpp 꿈돌이 2020.02.16 1201
54 dlib dnn_metric_learning_ex.cpp 꿈돌이 2020.02.07 4880
53 dlib dnn_metric_learning_on_images_ex.cpp 꿈돌이 2020.02.07 1404
52 dlib bearid 메모 꿈돌이 2020.02.03 1840
51 12864 결선 - OLED 0.96 결선 확인용 file 꿈돌이 2020.01.27 1256
50 ender 3 메모 꿈돌이 2020.01.05 321
49 Cura, PrusaSlicer (Slic3r) 포터블 꿈돌이 2019.12.25 172
48 Bed leveling 꿈돌이 2019.11.30 262
47 ms sql-server 명령 몇개 꿈돌이 2019.11.25 622
46 Klipper display config for SKR mini E3 v1.2 on ender 3 꿈돌이 2019.11.21 381
45 SKR mini E3 교체한 Ender 3에 Klipper OctoPrint 설치, 설정 및 적용 외 꿈돌이 2019.11.16 7520
44 윈도우에서 가상머신 또는 다른 곳으로 포트포워딩 꿈돌이 2019.09.02 454
43 윈도우10 배율 (dpi) 변경 스크립트 file 꿈돌이 2019.08.27 3934
42 윈도우 작업표시줄에 날짜와 시간 한 줄 표시, 요일 표시 꿈돌이 2019.08.20 465
41 윈도우 cmd에서 저장된 WIFI Profile의 비번 확인 꿈돌이 2019.07.03 2948
40 mysql update _priv='Y' root 복구용 꿈돌이 2019.06.25 395
39 리눅스 sh에서 실행파일 삭제 꿈돌이 2019.06.10 485
38 공중파 케이블 vod file 꿈돌이 2019.05.26 9374
37 윈도우10 앱 및 기능 에서 삭제 안되는 프로그램을 목록에서 강제로 제거하기 꿈돌이 2019.05.02 12539