關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • 在 Shell 傳給變數給 PHP

    Posted on June 21st, 2006 Sam Tang No comments

    先前貼出的文章 "文字模式下執行 PHP" 介紹了在 Shell 下執行 php 程式的方法,如果要在 shell 下面傳遞變數給 php,就像一般網頁用 GET 方法傳送一樣,系統會出現錯誤,例如:

    php -q shell_script.php?var=foo

    以上指令看似沒有問題,但系統會以為你要執行的 php 程式是 "shell_script.php?var=foo",而不是 "shell_script.php"。如果要傳送變數給 php,一個簡單的方法是用文字模式的瀏覽器,例如 lynx。而以下方法可以直接在指令上傳送:

    php -q shell_script.php -var foo -var2 foo2

    在 shell_script.php 裡面,執行 print_r($_SERVER["argv"]),結果應該會是:

    array (
              -var,
              foo,
              -var2,
              foo2
    )


    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word