[英]Can I pass an array to a php function that accepts an infinite number of arguments, in PHP 5.5 or earlier?
[英]Pass potentially infinite number of arguments to array except a single numeric variable in a PHP command line script
我正在嘗試制作一個PHP腳本,該腳本將使用命令行中潛在的無限數量的URL作為參數。 我還需要傳遞一個只能包含單個數字值的參數(以指定超時),例如:
./urltest.php 60 url1.com url2.com url3.com
我不確定如何將argv [1]指定為單個數字變量,而同時其余參數(即url列表)進入數組。 也許像這樣:
$timeout = $argv[1];
$args = func_get_args();
function numfilter($num) {
return !is_numeric($num);
}
$urls = array_filters($args, 'numfilter');
?
提前致謝!
我認為您不需要func_get_args
。 那是為了獲取當前函數的所有參數。 它與命令行無關。 我會像
//i don't like altering the global $argv
$urls = $argv;
//take off the first element of the array, leaving only urls
$script = array_shift($argv); //shift off script name, per other post :)
$timeout = array_shift($urls);
if(!is_numeric($timeout)) exit ("First argument was not a number") //array_shift always makes me break out perl style, so this however you want
//your $urls variable now contains an array with arguments 2 - n
請確保對array_shift進行兩次調用,因為argv [0]將是腳本的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.