Website Development Prices

Search Blog

Saturday, March 19, 2016

Definisanje podrazumevanih argumenata funkcije (Defining default arguments of function)

U prethodnom clanku videli smo kako mozemo da funkciji prosledimo podatke.

Primer:

<?php

ucim_php("Ja ucim PHP.");

function ucim_php($tekst)
{
echo $tekst, "<br />";

}

?>

Rezultat:

Ja ucim PHP.

Ako ovoj funkciji zaboravite da prosledite argument i pozovete je 

<?php

ucim_php();

function ucim_php($tekst)
{
     echo $tekst, "<br />";
}

?>

dobicete poruku o gresci


Warning: Missing argument 1 for ucim_php(), called in C:\xampp2\htdocs\PHPtuts\podrazumevani-argumenti-funkcije.php on line 21 and defined in C:\xampp2\htdocs\PHPtuts\podrazumevani-argumenti-funkcije.php on line 23

Da biste ovo izbegli, funkciji mozete da zadate i podrazumevane argumente, koji se koriste ako ne posaljete ni jednu vrednost. Ovo mozete da uradite pomocu znaka jednako = i same vrednosti.

Primer 2:

<?php

ucim_php();

function ucim_php($tekst = "Ja ucim PHP funkcije.")
{
echo $tekst, "<br />";

}

?>

Rezultat

Ja ucim PHP funkcije.

Napomena: ako funkciji prosledite podatke, podrazumevana vrednost se nece koristiti.

Podrazumevane vrednosti mozete da zadate za vise argumenata, ali kada pocenete da dodeljujete podrazumevane vrednosti, morate da ih zadate za sve argumente koji slede, zato sto ce se PHP "zbuniti" ako ne dostaje vise parametara.

Primer 3:

<?php

function ucim_php($tekst1, $tekst2 = "Ja ucim ", $tekst = "PHP.")
{
echo $tekst1, $tekst2, $tekst3, "<br />";

}

?>

Rezultat:

prazna strana

Primer 4: 

<?php

echo "<h1>Dobrodosli na Beogradski Plesni Festival</h1>";

rezervacija("Lorenzo");
rezervacija("Rossi", 2);
rezervacija("Marquez", 3, false);

function rezervacija($ime, $broj = 2, $balkon = true)
{
echo " Dobrodosli, $ime, imate rezervaciju za $broj" . " karte";
if ($balkon)
echo " na balkonu.";
else
echo " u parteru.";
}

?>

Rezultat:



In the previous article we saw how we can pass data to function.

Example:

<?php

learning_php("I am learning PHP.");

function learning_php($text)
{
echo $text, "<br />";

}

?>

Result:

I am learning PHP.

If you forget to submit argument to this function and you call it

<?php

learning_php();

function learning_php($text)
{
echo $text, "<br />";

}

?>

you will receive an warning message

Warning: Missing argument 1 for learning_php(), called in C:\xampp2\htdocs\PHPtuts\podrazumevani-argumenti-funkcije.php on line 31 and defined in C:\xampp2\htdocs\PHPtuts\podrazumevani-argumenti-funkcije.php on line 33

To avoid this, you can also provide the default arguments to the function, which are used if you do not send any value. This you can do with equal sign = and value itself.

Example 2:

learning_php();

function learning_php($text = "I am learning PHP functions.")
{
echo $text, "<br />";
}

Result

I am learning PHP functions.

Note: if you forward data  the function, the default value is not used.

Default values can be made for more arguments, but when you start to assign default values, you need to set them for all the arguments that follow, because PHP will be "confused" if more parameters is missing.

Example 3:

<?php

function learning_php($text1, $text2 = "I am learning ", $text = "PHP.")
{
echo $text1, $text2, $text3, "<br />";

}

?>


Result:

empty page

Example 4:

<?php

echo "<h1>Welcome to Belgrade Dance Festival</h1>";

reservation("Lorenzo");
reservation("Rossi", 2);
reservation("Marquez", 3, false);

function reservation($name, $number = 2, $balcony = true)
{
echo " Welcome, $name, you have a reservation for $number" . " tickets";
if ($balcony)
echo " on the balcony.";
else
echo " in the parterre.";

}

?>

Result:



No comments:

Post a Comment

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