2010-01-22 3 views

risposta

32

Sì. Utilizzare func_num_args() e func_get_arg() per ottenere gli argomenti:

<?php 
    function dynamic_args() { 
     echo "Number of arguments: " . func_num_args() . "<br />"; 
     for($i = 0 ; $i < func_num_args(); $i++) { 
      echo "Argument $i = " . func_get_arg($i) . "<br />"; 
     } 
    } 

    dynamic_args("a", "b", "c", "d", "e"); 
?> 

In PHP 5.6+ è ora possibile utilizzare variadic functions:

<?php 
    function dynamic_args(...$args) { 
     echo "Number of arguments: " . count($args) . "<br />"; 
     foreach ($args as $arg) { 
      echo $arg . "<br />"; 
     } 
    } 

    dynamic_args("a", "b", "c", "d", "e"); 
?> 
+2

abbastanza sicuro 'func_get_args () 'è ciò che in realtà si desidera utilizzare al posto di questa prima soluzione dettagliata. – Toskan

0

io vi incoraggio a passare un array nella vostra funzione, in questo modo si possono avere tanti parametri diversi memorizzati in quella matrice. Ci sono molte operazioni che puoi fare sulla matrice una volta che è nella funzione per ottenere le giuste informazioni che ti servono.

6

È possibile accettare un numero variabile di argomenti per qualsiasi funzione, purché vi siano abbastanza per popolare tutti gli argomenti dichiarati.

<?php 

function test ($a, $b) { } 

test(3); // error 
test(4, 5); // ok 
test(6,7,8,9) // ok 

?> 

Per accedere agli argomenti in più senza nome passati a test(), si utilizzano le funzioni func_get_args(), func_num_args() e func_get_arg($i):

<?php 

// Requires at least one param, $arg1 
function test($arg1) { 

    // func_get_args() returns all arguments passed, in order. 
    $args = func_get_args(); 

    // func_num_args() returns the number of arguments 
    assert(count($args) == func_num_args()); 

    // func_get_arg($n) returns the n'th argument, and the arguments returned by 
    // these functions always include those named explicitly, $arg1 in this case 
    assert(func_get_arg(0) == $arg1); 

    echo func_num_args(), "\n"; 
    echo implode(" & ", $args), "\n"; 

} 

test(1,2,3); // echo "1 & 2 & 3" 

?> 
1

mi piace di adottare un approccio Javascript-esque con i miei parametri PHP . Ciò consente una migliore impostazione delle "opzioni" e dei loro valori di default (che guarderò momentaneamente). Ad esempio, supponiamo di avere una funzione che restituisce un intervallo di volte in un array. Il parametro 1 è l'ora di inizio, il parametro 2 è l'ora di fine, il parametro 3 è l'intervallo di tempo e ogni opzione dopo è opzionale (come "format" => "24 ore", "include_seconds" => TRUE, ecc.).

vorrei definire la funzione in questo modo:

function returnTimeInterval($startTime, $endTime, $interval, $options = array()) 
{ 
    // the first thing to do is merge options in with our defaults 
    $options = array_merge(array(
     "format" => "24-hour", 
     "include_seconds => TRUE 
     // etc. 
    ), $options); 

Questo permette l'impostazione di default all'interno della funzione che può essere poi ignorato, che è piuttosto fresco. Certo, devi fare attenzione a non passare le strane opzioni non utilizzate, ma te ne lascio questo. :)

+0

'array_intersect_key()' aiuterà a evitare le opzioni sconosciute. – Walf

2

o solo

function printArgs() { 

foreach (func_get_args() as $arg){ 
    echo $arg; 
} 
} 

printArgs("1 ", "2 ", "three "); 

uscite 1 2 three

2

Anche se questa domanda è molto antica: In realtà in PHP 5.6+ è possibile scrivere esattamente quello che hai scritto: D