2011-10-06 44 views
19

執行PHP塊說我有一個代碼塊,我想測試這樣的:如何從終端不保存到一個文件

<?php 
Print "Hello, World!"; 
?> 

如何我趕緊跑從終端這個代碼,而無需將其保存到一個文件?

我試過的東西像...

php -r "Print "Hello, World!";" 

但剛剛接獲通知語法錯誤的抱怨。必須有一個簡單的方法來做到這一點。我還沒有找到任何解釋。

回答

30

轉義您正在用來劃定字符串的內部雙引號(")。

php -r "Print \"Hello, World!\";" 

或者,使用單引號(')作爲PHP字符串或用於引用PHP代碼。

如果您運行的是php --help,則可以看到php程序接受的命令列表。

-a    Run as interactive shell 
    -c <path>|<file> Look for php.ini file in this directory 
    -n    No php.ini file will be used 
    -d foo[=bar]  Define INI entry foo with value 'bar' 
    -e    Generate extended information for debugger/profiler 
    -f <file>  Parse and execute <file>. 
    -h    This help 
    -i    PHP information 
    -l    Syntax check only (lint) 
    -m    Show compiled in modules 
    -r <code>  Run PHP <code> without using script tags <?..?> 
    -B <begin_code> Run PHP <begin_code> before processing input lines 
    -R <code>  Run PHP <code> for every input line 
    -F <file>  Parse and execute <file> for every input line 
    -E <end_code> Run PHP <end_code> after processing all input lines 
    -H    Hide any passed arguments from external tools. 
    -S <addr>:<port> Run with built-in web server. 
    -t <docroot>  Specify document root <docroot> for built-in web server. 
    -s    Output HTML syntax highlighted source. 
    -v    Version number 
    -w    Output source with stripped comments and whitespace. 
    -z <file>  Load Zend extension <file>. 

    args...   Arguments passed to script. Use -- args when first argument 
        starts with - or script is read from stdin 

    --ini   Show configuration file names 

    --rf <name>  Show information about function <name>. 
    --rc <name>  Show information about class <name>. 
    --re <name>  Show information about extension <name>. 
    --rz <name>  Show information about Zend extension <name>. 
    --ri <name>  Show configuration for extension <name>. 
+0

它輸出的PHP 5.4 –

33

用於快速訪問PHP在碼頭先安裝PHP,然後運行這個:

php -a 

詳細

enter image description here

php -a打開了類型交互的shell直接php命令並查看結果立即,例如在之後在linux shell中你可以輸入echo 'Hello World';,並且按回車後Hello World!將被打印在屏幕上。

的Windows解決方案

在Windows

沒有交互模式相同的Linux事業的窗戶不能讀取命令行線,但仍然可以使用互動模式一樣!所以在Windows中打開的PHP的地方,你安裝它,例如,如果你使用xampp php是C:\xampp\php,然後鍵入php -a就像你在終端中輸入的內容,但是在每個部分的結尾你想查看結果只需按Ctrl+Z,然後按回車。

php -a 
echo 'hello world!'; 
^Z 
+1

錯誤這是更好地使用小寫形式:'PHP -a',這樣就可以在所有的操作系​​統上運行的命令。 – Farahmand

+0

這可能是一個更好的答案,如果它解釋了什麼'PHP -a'實際上 – Damon

+0

@Damon tnx達蒙,我添加了一些細節。 –

相關問題