Website Development Prices

Search Blog

Thursday, May 26, 2016

Kreiranje uslovnih funkcija (Creating conditional functions)

PHP je jezil koji se interpretira, sto znaci da se funkcije deklarisane unutar uslovnih iskaza, kao sto je iskaz if, ne mogu pozvati sve dok PHP interpreter ne izvrsi taj uslovni iskaz i vidi tu funkciju.

Primer: ako imamo funkciju postojeca_funkcija():

function postojeca_funkcija()
{
echo "postojeca_funkcija(): Ja sam spreman za pokretanje cim skripta pocne.<br />";
}

Nastavak primera: ova funkcija se moze pozvati cim se ovaj skript pokrene:


<?php

function postojeca_funkcija()
{
echo "postojeca_funkcija(): Ja sam spreman za pokretanje cim skripta pocne.<br />";
}

postojeca_funkcija();

?>

Rezultat:

postojeca_funkcija(): Ja sam spreman za pokretanje cim skripta pocne.

Primer 2: ako je funkcija deklarisana unutar iskaza if, PHP ce moci da nadje funkciju samo ako se izvrsi iskaz if. U tom slucaju vrednost promenljive $napravi_funkciju je TRUE

if ($napravi_funkciju){
function napravljena_funkcija()
{
echo "napravljena_funkcija(): Nisam spreman dok se iskaz if ne izvrsi.";
}

}

Nastavak primera 2: interpreter nece videti funkciju sve dok ne izvrsi telo if iskaza. Zato, ako zelite da pozovete funkciju, morate biti sigurni da je vrednost promenljive $napravi_funkciju TRUE.

if ($napravi_funkciju){
napravljena_funkcija();

}

Primer 3: pozivanje uslovne funkcije, ali samo nakon sto se izvrsi iskaz if:

<?php

function postojeca_funkcija()
{
echo "postojeca_funkcija(): Ja sam spreman za pokretanje cim skripta pocne.<br />";
}

postojeca_funkcija();

$napravi_funkciju = TRUE;

if ($napravi_funkciju){
function napravljena_funkcija()
{
echo "napravljena_funkcija(): Nisam spreman dok se iskaz if ne izvrsi.<br />";
}


if ($napravi_funkciju){
napravljena_funkcija();
}

?>

Rezultat:

postojeca_funkcija(): Ja sam spreman za pokretanje cim skripta pocne.
napravljena_funkcija(): Nisam spreman dok se iskaz if ne izvrsi.


PHP is language, which is interpreted to indicate that the functions are declared within a conditional statement, such as the statement if, can not call until PHP interpreter does not fulfill the conditional statement and see that function.

Example: if we have a function existing_function():

function existing_function()
{
echo "existing_function(): I'm ready to run as soon as the script begins.<br />";

}

Example continued: this function can be called as soon as the script begins:

function existing_function()
{
echo "existing_function(): I'm ready to run as soon as the script begins.<br />";
}


existing_function();

Result:

existing_function(): I'm ready to run as soon as the script begins.

Example 2: if the function is declared in the statement if, PHP will be able to find a function only if the statement if executes. In this case, the value of variable $create_function is TRUE.

if ($create_function){
function created_function()
{
echo "created_function(): I'm not ready until the if statement executes.<br />";
}

}

Example continued 2: interpreter will not see the function until executes the body of statement if. Therefore, if you want to call a function, you must make sure that the value of variable $create_function is TRUE.

if ($create_function){
created_function();

}

Example 3: calling conditional function, but only after the statement if executes:

<?php

function existing_function()
{
echo "existing_function(): I'm ready to run as soon as the script begins.<br />";
}

existing_function();

$create_function = TRUE;

if ($create_function){
function created_function()
{
echo "created_function(): I'm not ready until the if statement executes.<br />";
}


if ($create_function){
created_function();

}

?>

Result:

existing_function(): I'm ready to run as soon as the script begins.

created_function(): I'm not ready until the if statement executes.

No comments:

Post a Comment

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