Website Development Prices

Website Development Prices

Saturday, March 12, 2016

Kreiranje funkcije (Creating function)

Sintaksa za kreiranje funkcije:

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

}

Primer: na svim vasim web stranicama postoji navigacioni meni  sa hiperlinkovima koji se nalazi na dnu strane. Umesto da sav kod koji je potreban ubacujete na svaku web stranicu, mozete da napravite funkciju, koju mozete nazvati navigacioni_meni, da ona to radi umesto vas. Iskazi koji postoje u ovoj funkciji ce se izvrsiti u trenutku njenog poziva. Dodacemo echo iskaze koji salju HTML kod za navigacioni meni i hiperlinkove nazad do pretrazivaca. "&nbsp" u HTML kod ubacuje prazne karaktere da bi linkovi bili odvojeni.


<?php

function navigacioni_meni()
{
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 "<center>";

}

?>

Ako sada pozovete ovu funkciju, prikazace se navigacioni meni. Tako, ako treba nesto da promenite, menjate samo na jednom mestu, u funkciji navigacioni_meni.

Nastavak primera: pozivanje funckije.

<?php

echo "<h3>Kreiranje funkcije</h3>";
echo "<br />";
echo "Moja prva funkcija";
echo "<br />";
echo "<br />";

navigacioni_meni();

function navigacioni_meni()
{
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 "<center>";

}

?>

Rezultat:



Napomena: kod koji se nalazi  u funkcijama nece se izvrsiti sve dok se funkcije ne pozovu.

The syntax for creating a function:

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

}

Example: on all of your web pages there is a navigation menu with hyperlinks located on the bottom of the page. Instead of inserting all the code that is necessary, on every web page, you can create a function, you can call it navigation_menu, so that she can do it for you. The statements that are in this function will be executed at the time of her call. We'll add echo statements that send HTML code for navigation menu and hyperlinks back to the browser. "&nbsp" in the HTML code inserts blank characters so the links are separated.

<?php

function navigation_menu()
{
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 "<center>";

}

?>

If we call this function, the navigation menu will show. So, if you want something to change, you can change it only at one place, in the function navigation_menu.

Example continued: calling function.

<?php

cho "<h3>Creating function</h3>";
echo "<br />";
echo "My first function";
echo "<br />";
echo "<br />";

navigation_menu();

function navigation_menu()
{
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 "<center>";

}

?>

Result:




Note: the code contained in the functions will not be executed until the functions are not called.

No comments:

Post a Comment

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