Kada se funkciji prosledjuje argument, podrazumevano se prosledjuje po vrednosti. To znaci da se u funkciju salje kopija podatka a ne sam podatak.
Ako zelite da promenite originalnu vrednost koju ste prosledili u funkciju, na primer, ako zelite stringu koji je usao u funkciju dodate neki tekst:
Primer:
<?php
$string = "Ja volim ";
dodaj_tekst($string);
echo $string;
function dodaj_tekst($tekst)
{
$tekst .= "motore.";
}
?>
Rezultat:
Ja volim
Ako ispred argumenta koji saljete u funkciju dodate znak &, PHP ce argument proslediti po referenci.
Nastavak primer:
$string = "Ja volim ";
dodaj_tekst($string);
echo $string;
function dodaj_tekst(&$tekst)
{
$tekst .= "motore.";
}
?>
Rezultat:
Ja volim motore.
Objasnjenje: sada je u funkciju dodaj_tekst prosledjena referenca na originalan podatak ($string), sto znaci da u funkciji mozete da pristupite originalnom podatku.
When you pass argument to the function, by default is passed by value. This means that the function sends a copy of the data rather than the data itself.
If you want to change the original value that you pass to the function, for example, if you want, to a string that entered in the function, add some text:
Example:
<?php
$string = "I love ";
add_text($string);
echo $string;
function add_text($text)
{
$text .= "motorcycles.";
}
?>
Result:
I love
If in the front of the argument that you are sending to the function. add ampersand &, PHP will pass the argument by reference.
Continued example:
<?php
$string = "I love ";
add_text($string);
echo $string;
function add_text(&$text)
{
$text .= "motorcycles.";
}
?>
Result:
I love motorcycles.
Explanation: now is the function add_text passed a reference to the original data ($string), which means that in the function you can access the original data.
Ako zelite da promenite originalnu vrednost koju ste prosledili u funkciju, na primer, ako zelite stringu koji je usao u funkciju dodate neki tekst:
Primer:
<?php
$string = "Ja volim ";
dodaj_tekst($string);
echo $string;
function dodaj_tekst($tekst)
{
$tekst .= "motore.";
}
?>
Rezultat:
Ja volim
Ako ispred argumenta koji saljete u funkciju dodate znak &, PHP ce argument proslediti po referenci.
Nastavak primer:
$string = "Ja volim ";
dodaj_tekst($string);
echo $string;
function dodaj_tekst(&$tekst)
{
$tekst .= "motore.";
}
?>
Rezultat:
Ja volim motore.
Objasnjenje: sada je u funkciju dodaj_tekst prosledjena referenca na originalan podatak ($string), sto znaci da u funkciji mozete da pristupite originalnom podatku.
When you pass argument to the function, by default is passed by value. This means that the function sends a copy of the data rather than the data itself.
If you want to change the original value that you pass to the function, for example, if you want, to a string that entered in the function, add some text:
Example:
<?php
$string = "I love ";
add_text($string);
echo $string;
function add_text($text)
{
$text .= "motorcycles.";
}
?>
Result:
I love
If in the front of the argument that you are sending to the function. add ampersand &, PHP will pass the argument by reference.
Continued example:
<?php
$string = "I love ";
add_text($string);
echo $string;
function add_text(&$text)
{
$text .= "motorcycles.";
}
?>
Result:
I love motorcycles.
Explanation: now is the function add_text passed a reference to the original data ($string), which means that in the function you can access the original data.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.