2013-11-23 69 views
1

我使用WAMP,並且自從學習了PHP之後,我一直在運行我的php腳本,通過在網頁上查看輸出。例如,要查看名爲script.php的腳本中的輸出,我將使用localhost/script.php。PHP:無需在網頁上運行PHP腳本?

有沒有更好的方法來做到這一點?我的意思是,在Java中有Eclipse,你可以點擊綠色按鈕,它會爲你運行代碼並看到即時輸出。有沒有像這樣的PHP?

回答

2

有可能不通過Web服務器運行在命令行PHP腳本。要做到這一點添加下面的邏輯來你的腳本:

if (defined('STDIN')) { 
    if (isset($argv)){ 
     // handle your command line arguments here with getopt 
    } 
} 
// GET request parameter definitions // 
else { 
    // handle your URL parameters (via GET or POST requests) here 
} 

當腳本命令行運行與PHP解釋器

php myfile.php -s --longflag <argument> 

STDIN定義,你可以處理命令行開關,旗幟,以及在if塊中帶有getopt的參數。

當您通過Web服務器上的URL訪問該腳本時,該腳本到達else塊。您當前擁有的PHP代碼可以放在該塊中。

下面是我的一個項目,演示瞭如何處理URL參數,或長或短的命令行選項的示例:

// Command line parameter definitions // 
if (defined('STDIN')) { 
    // check whether arguments were passed, if not there is no need to attempt to check the array 
    if (isset($argv)){ 
     $shortopts = "c:"; 
     $longopts = array(
      "xrt", 
      "xrp", 
      "user:", 
     ); 
     $params = getopt($shortopts, $longopts); 
     if (isset($params['c'])){ 
      if ($params['c'] > 0 && $params['c'] <= 200) 
       $count = $params['c']; //assign to the count variable 
     } 
     if (isset($params['xrt'])){ 
      $include_retweets = false; 
     } 
     if (isset($params['xrp'])){ 
      $exclude_replies = true; 
     } 
     if (isset($params['user'])){ 
      $screen_name = $params['user']; 
     } 
    } 
} 
// Web server URL parameter definitions // 
else { 
    // c = tweet count (possible range 1 - 200 tweets, else default = 25) 
    if (isset($_GET["c"])){ 
     if ($_GET["c"] > 0 && $_GET["c"] <= 200){ 
      $count = $_GET["c"]; 
     } 
    } 
    // xrt = exclude retweets from the timeline (possible values: 1=true, else false) 
    if (isset($_GET["xrt"])){ 
     if ($_GET["xrt"] == 1){ 
      $include_retweets = false; 
     } 
    } 
    // xrp = exclude replies from the timeline (possible values: 1=true, else false) 
    if (isset($_GET["xrp"])){ 
     if ($_GET["xrp"] == 1){ 
      $exclude_replies = true; 
     } 
    } 
    // user = Twitter screen name for the user timeline that the user is requesting (default = their own, possible values = any other Twitter user name) 
    if (isset($_GET["user"])){ 
     $screen_name = $_GET["user"]; 
    } 
} // end else block 

我覺得這是對測試有幫助。希望能幫助到你。

1

Jetbrains的PHP風暴是一個很好的調試工具

1

如果您使用Sublime Text作爲文本編輯器,則可以使用XDebug