2016-05-26 20 views
0

我使用這個功能http://php.net/manual/en/function.getopt.php做到這一點for循環EXCUTE一個PHP代碼錯誤

$options = getopt(array('c::'), array('chunk::')); 
var_dump($options); 

這段代碼位於一個名爲do.php並使用該shell命令打印結果

for i in `seq 2`;do php /home/user/public_html/do.php --chunk=$i ;done 

結果是

array(2) { 
    ["chunk"]=> 
    string(1) "1" 
} 
array(2) { 
    ["chunk"]=> 
    string(1) "1" 
} 

怎麼說happend?應該

array(2) { 
    ["chunk"]=> 
    string(1) "1" 
} 
array(2) { 
    ["chunk"]=> 
    string(1) "2" 
} 

(PHP版本5.4.45)

+1

第一個參數o f getopt應該是一個字符串而不是數組。 使用php的絕對路徑。將數組('c ::')轉換爲'c ::'後,代碼在我的設置中工作得很好 –

回答

1

正如你可以在documentation看到的,getopt描述是:

array getopt (string $options [, array $longopts ]) 

所以第一個參數必須是string代替an array

<?php 
$options = getopt('c::', array('chunk::')); 
var_dump($options);