Website Development Prices

Search Blog

Saturday, May 14, 2016

Kreiranje promenljivih za funkcije (Creating variables for functions)

Promenljive za funkcije mogu da sadrze nazive funkcija, a funkcije pozivate tako sto cete iza promenljive dodati zagrade. Na osnovu toga, moze se u trenutku izvrsenja programa odrediti koja ce se funkcija pozvati.

Primer: ako imamo funkciju suzuki:

function suzuki()
{
echo "Sada smo u suzuki() <br />";
echo "Imamo puno suzuki motora.<br /><br />";
}

Nastavak primera:  da biste ovu funkciju pozvali pomocu promenljive za funkcije, potrebno je da promenljivoj dodelite vrednost "suzuki" i da onda funkciju pozovete pomocu promenljive:


<?php

$promenljiva_funkcije = "suzuki";
$promenljiva_funkcije();

function suzuki()
{
echo "Sada smo u suzuki() <br />";
echo "Imamo puno suzuki motora.<br /><br />";

}

?> 

Rezultat:

Sada smo u suzuki()
Imamo puno suzuki motora.


Ovakvim funkcijama mozete da prosledite i argumente, a mozete i da podesite podrazumevane vrednosti argumenata.

Primer 2:

<?php

$promenljiva_funkcije = "suzuki";
$promenljiva_funkcije();

$promenljiva_funkcije = "honda";
$promenljiva_funkcije("Sada smo u hondi().");

$promenljiva_funkcije = "ducati";
$promenljiva_funkcije("Sada smo u ducati().");

function suzuki()
{
echo "Sada smo u suzuki() <br />";
echo "Imamo puno suzuki motora.<br /><br />";
}

function honda()
{
echo "Sada smo u hondi() <br />";
echo "Takodje imamo puno honda motora.<br /><br />";
}

function ducati()
{
echo "Sada smo u ducati() <br />";
echo "Takodje imamo puno i ducati motora.<br /><br />";

}

?>

Rezultat:

Sada smo u suzuki()
Imamo puno suzuki motora.

Sada smo u hondi()
Takodje imamo puno honda motora.

Sada smo u ducati()
Takodje imamo puno i ducati motora.


The variables for the functions may contain the names of functions, and you call a  function by adding parentheses behind variables. On this basis, it can be at the moment of the execution of the program when you can determine which function to call.

Example: if we have a function suzuki:

function suzuki()
{
echo "We are in suzuki() now.<br />";
echo "We have a lot of suzuki motorcycles.<br /><br />";
}

Continuation of the example: to call this function you can use variable for functions, it is necessary to assign a value "suzuki"  to a variable and then to call the function using variable:

<?php

$function_variable = "suzuki";
$function_variable();

function suzuki()
{
echo "We are in suzuki() now.<br />";
echo "We have a lot of suzuki motorcycles.<br /><br />";
}

?>

Result:

We are in suzuki() now.
We have a lot of suzuki motorcycles.


To such functions you can pass arguments, and you can also set default values ​​of arguments.

Example 2:

<?php

$function_variable = "suzuki";
$function_variable();

$function_variable = "honda";
$function_variable("We are in honda().");

$function_variable = "ducati";
$function_variable("We are in ducati().");

function suzuki()
{
echo "We are in suzuki() <br />";
echo "We have a lot of suzuki motorcycles.<br /><br />";
}

function honda()
{
echo "We are in honda() <br />";
echo "Also we have a lot of honda motorcycles.<br /><br />";
}

function ducati()
{
echo "We are in ducati() <br />";
echo "Also we have a lot of ducati motorcycles.<br /><br />";

}

?>


Result:

We are in suzuki()
We have a lot of suzuki motorcycles.

We are in honda()
Also we have a lot of honda motorcycles.

We are in ducati()
Also we have a lot of ducati motorcycles.


No comments:

Post a Comment

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