Website Development Prices

Search Blog

Wednesday, April 27, 2016

Upotreba lokalnog domena promenljive (Using local scope of variable)

Domen promenljive je oblast u kodu u kojoj je ta promenljiva vidljiva. 

Primer: ako negde u kodu koristite promenljivu po imenu $brojac:

$brojac = 2;
.
.
.
.
.

Nastavak primera: ta ista promenljiva se koristi i kasnije:

$brojac = 2;
.
.
.
.
.

$brojac = 50;
.
.
.
.
.

Objasnjenje: dok kod bude rastao, moze se desiti da u njega uvedete novu promenljivu sa imenom $brojac, zaboravljajuci da vec imate promenljivu sa tim imenom, sto dovodi konfilkta jer promena jedne promenljive menja i drugu, zato sto za PHP to su iste promenljive. Resenje ovakvog konflikta su funkcije jer funkcija razlaze kod.


Ako u funkcijama koristite promenljive, njihov domen je ogranicen na te funkcije, sto je podrazumevano To znaci da ce prvi iskaz echo odstampati  3, dok ce drugi odstampati 130, posto je $vrednost.  u funkcji, u njenom domenu.

Primer 2:

<?php

$vrednost = 3;
echo "U kodu, \$vrednost je ", $vrednost, "<br />";

function lokalni_domen()
{
    $vrednost = 130;
    echo "U funkciji, \$vrednost je ", $vrednost, "<br />";

}

?>

Rezultat:

U kodu, $vrednost je 3

Primer 3: provera lokalnog domena kod funkcija - proveravamo istu promenljivu $vrednost unutar i izvan funkcije.

<?php

$vrednost = 3;
echo "U kodu, \$vrednost je ", $vrednost, "<br />";
lokalni_domen();
echo "U kodu opet, \$vrednost je ", $vrednost, "<br />";

function lokalni_domen()
{
    $vrednost = 130;
    echo "U funkciji, \$vrednost je ", $vrednost, "<br />";
}

?>

Rezultat:

U kodu, $vrednost je 3
U funkciji, $vrednost je 130
U kodu opet, $vrednost je 3


Objasnjenje: lokalna promenljiva $vrednost unutar funkcije nije na raspolaganju izvan funkcije. Lokalne promenljive u funkcijama su ogranicene na domen funkcije.

The scope of variable is the area code where that variable is visible.

Example: if somewhere in the code you are using a variable called $counter:

$counter = 2;
.
.
.
.
.

Continuation of the example: and that same variable is used later:

$counter = 2;
.
.
.
.
.

$counter = 50;
.
.
.
.
.

Explanation: while the code grows, it can happen that in code, you introduce a new variable called $counter, forgetting that already have a variable with that name, which results in conflict because changing one variable changes the second, because to the PHP they are the same variables. The solution of this conflict are functions because functions disassemble the code. 

If in the functions you are using variables, their scope is limited to this function, which is default. This means that the first echo statement will print 3, while the second will print the 130 because $value is in the function. in her scope.

Example 2:

<?php

$value = 3;
echo "In code, \$value is ", $value, "<br />";

function local_scope()
{
    $value = 130;
    echo "In function, \$value is ", $value, "<br />";

}

?>

Result:

In code, $value is 3

Example 3: checking local scope in functions - we will check the same variable $value inside and outside of the function.

<?php

$value = 3;
echo "In code, \$value is ", $value, "<br />";
local_scope();
echo "In code again, \$value is ", $value, "<br />";

function local_scope()
{
    $value = 130;
    echo "In function, \$value is ", $value, "<br />";

}

?>

Result:

In code, $value is 3
In function, $value is 130
In code again, $value is 3



Explanation: the local variable $value inside the function is not available outside the function. Local variables in functions are limited to the scope of the function

No comments:

Post a Comment

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