怎麼運行php腳本
使用Linux系統搭建完整的PHP環境後,用戶常會遇到執行PHP腳本需要使用php myscript.php的方式,感覺較為繁瑣。實際上,Linux系統支持直接執行PHP腳本文件。具體操作步驟如下:
首先,編寫PHP腳本文件。例如,創建名為test_run.php的文件,內容如下:
Here is some plain text.
Here is the file name:
《?php
echo $argv[0], PHP_EOL;
》
腳本功能簡單,輸出當前腳本文件的名稱。
接著,通過命令執行腳本:
yuanyu@ymac:phpworkspace $ php test_run.php hello
輸出結果為:
Here is some plain text.
Here is the file name:
test_run.php
yuanyu@ymac:phpworkspace $
為腳本文件增加頭信息及設置許可權:
在文件首行添加php命令全路徑,前綴為#!:
#!/usr/bin/php
保持腳本內容不變:
《?php
echo $argv[0], PHP_EOL;
》
執行賦予可執行許可權:
yuanyu@ymac:phpworkspace $ chmod u+x 。/test_run.php
即可直接執行腳本:
yuanyu@ymac:phpworkspace $ 。/test_run.php
輸出結果為:
Here is some plain text.
Here is the file name:
/test_run.php
yuanyu@ymac:phpworkspace $
此方法在PHP官方文檔中亦有提及,請參考:
http://php.net/manual/en/features.commandline.usage.php
文檔中關於腳本在命令行運行的示例,請參照:
「Example #2 Script intended to be run from command line (script.php)」
❷ 如何通過Linux命令行使用和運行PHP腳本
在安裝完PHP和Apache2後,需要安裝PHP命令行解釋器。
可以直接在Linux命令行使用 phpinfo()
這個十分有價值的調試工具而不需要從文件來調用,只需執行以下命令:
3. 以交互模式運行PHP並做一些數學運算。這里,『-a『 選項用於以交互模式運行PHP。
4. 可以僅僅將PHP腳本作為shell腳本來運行。首先,創建在當前工作目錄中創建一個PHP樣例腳本。
5. 可以完全靠自己通過交互shell來創建簡單函數。