Website Development Prices

Website Development Prices

Monday, March 14, 2016

Prosledjivanje podataka funkcijama (Passing data to functions)

Funkciji mozete da prosledite podatke pomocu liste argumenata. To je lista naziva promenljivih, koji su odvojeni zarezima.

function ime_funkcije([lista_argumenta...])
{
[iskazi];
[return vracena_vrednost];

}

Primer: ako zelite da prilagodite navigacioni meni koji smo napravili u prethodnom clanku, na primer, zelite da dodate neki tekst i oznaku autorskih prava. To mozete da uradite tako sto cete u funkciju poslati te dve stavke. Ako bismo te dve stavke u telu funkcije oznacili kao $tekst i $autorska_prava, onda bi funkcija mogla da izgleda ovako:


<?php

function navigacioni_meni($tekst, $autorska_prava)
{
echo "<hr />";
echo "<center>";
echo "<a href='pocetna.html'>Pocetna</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='o-nama.html'>O Nama</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='usluge.html'>Usluge</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='kontakt.html'>Kontakt</a>";
echo "<hr />";
echo "<font size='3'><b>$tekst</b></font>";
echo "<br /><font size='3'><b>$autorska_prava</b></font>";
echo "<center>";

}

?>

Ako zelite da pozovete ovu novu verziju funkcije prosledite tekst "Moja Kompanija" i "(c) 2016.". Ove tekstualne stavke ce se smestiti u promenljive $tekst i $autorska_prava.

Nastavak primera: prosledjivanje podataka u funkciju.

<?php

echo "<h3>Prosledjivanje podataka funkciji</h3>";
echo "<br />";
echo "Moja druga funkcija";
echo "<br />";
echo "<br />";

navigacioni_meni("Moja Kompanija", "&copy; 2016.");

function navigacioni_meni($tekst, $autorska_prava)
{
echo "<hr />";
echo "<center>";
echo "<a href='pocetna.html'>Pocetna</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='o-nama.html'>O Nama</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='usluge.html'>Usluge</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='kontakt.html'>Kontakt</a>";
echo "<hr />";
echo "<font size='3'><b>$tekst</b></font>";
echo "&nbsp;|&nbsp;<font size='3'><b>$autorska_prava</b></font>";
echo "<center>";

}

?>

Rezultat:



Objasnjenje: funkcija navigacioni_meni moze da procita podatke koje ste poslali i da ih ubaci u navigacioni meni.

To the function you can pass data using the argument list.  This is a list of variable names, separated by commas.

function function_name([argument_list...])
{
[statements];
[return return_value];

}

Example: if you want to customize the navigation menu that we made in the previous article, for example, you want to add some text and the copyright notice. You can do this by, passing this two items to the function. If we mark these two items in the body of function as $text and $copyright, then the function might look like this:

<?php

function navigation_menu($text, $copyright)
{
echo "<hr />";
echo "<center>";
echo "<a href='home.html'>Home</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='about.html'>About</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='services.html'>Services</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='contact.html'>Contact</a>";
echo "<hr />";
echo "<font size='3'><b>$text</b></font>";
echo "<br /><font size='3'><b>$copyright</b></font>";
echo "<center>";

}

?>

If you want to call this new version of the function pass the text "My Company" and "(c) 2016.". These text items will be stored in the variables $text and $copyright.

Example continued: passing data to function.

<?php

echo "<h3>Passing data to function</h3>";
echo "<br />";
echo "My second function";
echo "<br />";
echo "<br />";

navigation_menu("My Company", "&copy; 2016.");

function navigation_menu($text, $copyright)
{
echo "<hr />";
echo "<center>";
echo "<a href='home.html'>Home</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='about.html'>About</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='services.html'>Services</a>&nbsp;&nbsp;&nbsp;";
echo "<a href='contact.html'>Contact</a>";
echo "<hr />";
echo "<font size='3'><b>$text</b></font>";
echo "&nbsp;|&nbsp;<font size='3'><b>$copyright</b></font>";
echo "<center>";

}

?>

Result:




Explanation: function navigation_menu can read the data you send and to put them in the navigation menu.

No comments:

Post a Comment

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