Website Development Prices

Search Blog

Friday, April 15, 2016

Kreiranje promenljive liste argumenata (Creating a variable list of arguments)

U funkcije u PHP-u mozete proslediti promenljivi broj argumenata, sto nije isto kao definisanje podrazumevanih argumenata. Mozete istu funkciju da pozivate razlicitim brojem argumenata. Sve argumente mozete preuzeti, pomocu specijalne funkcije, a ne pomocu podrazumevanih vrednosti. Mozete proslediti koliko god zelite argumenata.

Primer: ako imate funkciju joiner, koja spaja stringove, mozete je pozvati ovako:

joiner("Ja", "volim");
joiner("Ja", "volim", "motore.");

joiner("Ja", "volim", "Suzuki", "motore.");

U funkciji joiner mozete koristiti jednu od tri PHP funkcije za dobijanje broja argumenata, svakog od argumenata i niza koji sadrzi sve argumente koje ste prosledili. 


func_num_args();     Vraca broj prosledjenih argumenata.
func_get_arg();     Vraca jedan argument.  

func_get_args();     Vraca sve argumente u obliku niza,

Primer 2: koristicemo funckiju func_num_args za dobijanje broja argumenata i funkciju func_get_args za dobijanje svih argumenata u obliku niza.

function joiner()
{
$tekst = "";
$lista_argumenata = func_get_args();

for($indeks_loop = 0; $indeks_loop < func_num_args(); $indeks_loop++) {
$tekst .= $lista_argumenata[$indeks_loop] . " ";
}
echo $tekst;

}


Nastavak primera 2: pozivanje funkcije

<?php

echo "joiner(\"Ja\", \"volim\") = ", joiner("Ja", "volim"), "<br />";
echo "joiner(\"Ja\", \"volim\", \"motore.\") = ", joiner("Ja", "volim", "motore."), "<br />";
echo "joiner(\"Ja\", \"volim\", \"Suzuki\", \"motore.\") = ", joiner("Ja", "volim", "Suzuki", "motore."), "<br />";

function joiner()
{
$tekst = "";
$lista_argumenata = func_get_args();

for($indeks_loop = 0; $indeks_loop < func_num_args(); $indeks_loop++) {
$tekst .= $lista_argumenata[$indeks_loop] . " ";
}
echo $tekst;

}

?>

Rezultat: 

joiner("Ja", "volim") = Ja volim
joiner("Ja", "volim", "motore.") = Ja volim motore.
joiner("Ja", "volim", "Suzuki", "motore.") = Ja volim Suzuki motore. 


Ako zelite da dobijete samo jedan argument mozete koristiti funkciju func_get_arg. Njoj treba da prosledite poziciju argumenta koji zelite, brojanje pocinje od 0 (nula), a ona ce vratiti njegovu vrednost. 

In the functions in PHP you can pass a variable number of arguments, which is not the same as the definition of default arguments. You can call the same function with a different number of arguments. All arguments can be displayed, using a special function, and not using the default values. You can pass as much as you want arguments.

Example: if you have a function joiner, which connecta the strings, you can call like this:

joiner("I", "love");
joiner("I", "love", "motorcycles.");

joiner("I", "love", "Suzuki", "motorcycles.");

In the function joiner you can use one of three PHP functions to obtain the number of arguments, each of the arguments and the array containing all the arguments that you pass.

func_num_args();      Returns the number of submitted arguments.
func_get_arg();     Returns  an argument.

func_get_args();     Returns all arguments as an array.

Example 2: we will use the function func_num_args to obtain the number of arguments and the function func_get_args to get all arguments as an array.

function joiner()
{
$text = "";
$arguments_list = func_get_args();

for($index_loop = 0; $index_loop < func_num_args(); $index_loop++) {
$text .= $arguments_list[$index_loop] . " ";
}
echo $text;

}

Continued Example 2: calling function

<?php

echo "joiner(\"I\", \"love\") = ", joiner("I", "love"), "<br />";
echo "joiner(\"I\", \"love\", \"motorcycles.\") = ", joiner("I", "love", "motorcycles."), "<br />";
echo "joiner(\"I\", \"love\", \"Suzuki\", \"motorcycles.\") = ", joiner("I", "love", "Suzuki", "motorcycles."), "<br />";

function joiner()
{
$text = "";
$arguments_list = func_get_args();

for($index_loop = 0; $index_loop < func_num_args(); $index_loop++) {
$text .= $arguments_list[$index_loop] . " ";
}
echo $text;

}

?>

Result:

joiner("I", "love") = I love
joiner("I", "love", "motorcycles.") = I love motorcycles.
joiner("I", "love", "Suzuki", "motorcycles.") = I love Suzuki motorcycles. 


If you want to get only one argument you can use the function func_get_arg. You need to pass position of the argument you want, counting starts from 0 (zero), and it will return its value.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.