Professional Documents
Culture Documents
PHP Kontrola Toka Funkcije
PHP Kontrola Toka Funkcije
Božidar Kovačić
Filozofski fakultet
Omladinska 14
51000 Rijeka
bkovacic@ffri.hr
PHP SERVERSKI SKRIPTNI
JEZIK
PHP – kontrola toka
2
Funkcije
Pisanje funkcija
Rekurzija
<?php
Uporaba if-else $sky_color = "blue";
$temp_fahrenheit = "65";
if($sky_color == "blue")
{
print("The sky is nice today! <br />");
if($temp_fahrenheit >= 70)
{
print("The temperature is wonderful.<br />");
print("Perhaps a picnic or bike ride is in order!
<br>");
}
else
{
print("It is a bit nippy.<br>");
print("Perhaps a jacket would be good.<br
/>");
}
}
else
{
print("The sky is not blue today.<br />");
}
?>
<?php
Ugniježdavanje $x = 10;
$z = $x;
if blokova
print("<pre>");
// Style #1
if($x == $z)
{
print("<pre>");
print("if (...)\n{\n ... code ... \n}\n\n\n\n");
print("</pre>");
}
// Style #2
if($x == $z) {
print("<pre>");
print("if (...) \n{\n ... code ... \n}\n\n\n\n");
print("</pre>"); }
print("</pre>");
Switch
switch (varijabla)
{
case “vrijednost 1”:
naredba ako je varijabla = vrijednost 1
case “vrijednost 2”:
naredba ako je varijabla = vrijednost 2
case “vrijednost 3”:
naredba ako je varijabla = vrijednost 3
…
default:
naredba za svaku drugu nenavedenu vrijednost
}
Uporaba switch
<?php
// Seed random number generator
srand((double)microtime() * 1000000);
print("If-elseif-else: ");
if($letter == "a") { print("The letter is a vowel!<br />"); }
else if($letter == "e") { print("The letter is a vowel!<br />"); }
else if($letter == "i") { print("The letter is a vowel!<br />"); }
else if($letter == "o") { print("The letter is a vowel!<br />"); }
else if($letter == "u") { print("The letter is a vowel!<br />"); }
else { print("The letter is NOT a vowel!<br />"); }
Uporaba switch
print("Switch: ");
switch($letter)
{
case "a":
case "e":
case "i":
case "o":
case "u":
// Notice if $letter is not 'u', it 'falls' through to here
print("The letter is a vowel!<br />");
break;
default:
print("The letter is NOT a vowel!<br />");
break;
}
?>
While petlja
While petlja
<html>
<head>
<title>While</title>
</head>
<body>
<h3>Alphabet Soup</h3>
<?php
// Seed random number generator
srand((double)microtime() * 1000000);
Do-while petlja
do
{
naredba koja se izvodi najmanje jednom, a onda ponavlja ovisno o
ispunjenju uvjeta za provjeru
} while (uvjet za provjeru);
Petlja for
Petlja foreach
Petlja foreach
<?php
$prog_language[] = "PHP";
$prog_language[] = "C";
$prog_language[] = "C++";
$prog_language[] = "Java";
$prog_language[] = "Forth";
$prog_language[] = "Perl";
$prog_language[] = "Python";
$prog_language[] = "Ruby";
foreach($prog_language as $language)
{
if(!strcmp($language, "PHP"))
print("<b>" . $language . "</b> <br />");
else
print($language . " <br />");
}
?>
Iskaz break
Izraz za napuštanje petlje
Iskaz continue
Iskaz za preusmjeravanje izvođenja na početak i
nastavak izvođenja nove iteracije
Iskaz exit
Iskaz za zaustavljanje izvođenja skripta i njegova
napuštanje
$num_iterations[$i] = $j;
}
$avg_iterations = (array_sum($num_iterations) / $outer_loop_itr);
print("It took an average of " . $avg_iterations . " iterations " .
"to exit the inner do-while loop <br />");
?>
</body>
</html>
Pisanje funkcija
function (argument1, argument2, …)
{
naredba 1;
…
return (vrijednost);
}
Rekurzija
<html>
<head>
<title>Recursion</title>
</head>
<body>
<?php
function cnt_backwards($from)
{
$from--;
if($from <= 1)
{
print($from);
return;
}
print($from);
cnt_backwards($from);
}
cnt_backwards(5);
?>
</body>
</html>
if(!strcmp($str_ctrl_flow, "go"))
Donošenje odluke {
print("The string is \"go\", so we should keep going <br />");
}
else
{
print("Time to stop processing this script <br />");
exit;
}
$str_ctrl_flow = "stop";
if(!strcmp($str_ctrl_flow, "go"))
{
print("The string is \"go\", so we should keep going <br />");
}
else
{
print("Time to stop processing this script <br />");
exit;
}
print("This won't print. <br />");
?>
</body>
</html>
include
<?php
include("math_lib.php");
?>
<html>
<head>
<title>Important Processing Page</title>
</head>
<body>
<?php
print(to_pow(6, 10) . "<br />");
print(multiply(5, 100) . "<br />");
print(divide(5, 0) . "<br />");
print(subtract(100, 50) . "<br />");
print(add(100, 50) . "<br />");
?>
</body>
</html>