Show sourcecode
The following files exists in this folder. Click to view.
ovn_fr.php
ovn_funk.php
ovn_gr_1-5.php
style/
ovn_funk.php
133 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
<?php
$title = "Övningar funktion";
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $title; ?></title>
<link rel="stylesheet" href="style/stylesheet.css">
</head>
<body>
<div class="code">
<?php
echo "<div class='task'>";
echo "<h2>Uppgift 1</h2>";
echo "<p>Skapa en funktion som skriver ut text.</p>";
function texty() {
echo "Hejsansvejsan! <br>";
}
texty();
echo "</div>";
echo "<div class='task'>";
echo "<h2>Uppgift 2</h2>";
echo "<p>Skapa en funktion som skriver ut text ett visst antal gånger.</p>";
function text(int $antal) {
for ($i = 1; $i <= $antal; $i++) {
echo "Hejsansvejsan! <br>";
}
}
text(8);
echo "</div>";
echo "<div class='task'>";
echo "<h2>Uppgift 3</h2>";
echo "<p>Hitta det största talet bland tre värden.</p>";
function numComp(int $tal1, int $tal2, int $tal3) {
if ($tal1 >= $tal2) {
if ($tal1 >= $tal3) {
return $tal1;
} else {
return $tal3;
}
} elseif ($tal2 >= $tal3) {
return $tal2;
} else {
return $tal3;
}
}
echo "<h3>största talet av 24, 34, 23</h3>";
$result = numComp(23, 34, 23);
echo "är $result";
echo "<h3>största talet av 50, 16, 67</h3>";
$result = numComp(50, 16, 67);
echo "är $result";
echo "</div>";
echo "<div class='task'>";
echo "<h2>Uppgift 4</h2>";
echo "<p>Hitta det största eller minsta talet beroende på parameter.</p>";
function numCompy(int $tal1, int $tal2, int $tal3, string $minsta = "") {
if ($minsta == "minsta") {
if ($tal1 <= $tal2) {
if ($tal1 <= $tal3) {
return $tal1;
} else {
return $tal3;
}
} elseif ($tal2 <= $tal3) {
return $tal2;
} else {
return $tal3;
}
} else {
if ($tal1 >= $tal2) {
if ($tal1 >= $tal3) {
return $tal1;
} else {
return $tal3;
}
} elseif ($tal2 >= $tal3) {
return $tal2;
} else {
return $tal3;
}
}
}
echo "<h3>största talet av 24, 34, 23</h3>";
$resulty = numCompy(23, 34, 23);
echo "är $resulty";
echo "<h3>minsta talet av 50, 16, 67</h3>";
$resulty = numCompy(50, 16, 67, "minsta");
echo "är $resulty";
echo "</div>";
echo "<div class='task'>";
echo "<h2>Uppgift 5</h2>";
echo "<p>Räkna ut medelvärdet för en lista med tal.</p>";
function averg(int ...$lista) {
$antal = count($lista);
$sum = 0;
for ($i = 0; $i < $antal; $i++) {
$sum += $lista[$i];
}
return $sum / $antal;
}
echo "Medelvärdet för 28 + 39 + 21 + 74:<br>";
echo averg(28, 39, 21, 74);
echo "</div>";
echo "<div class='task'>";
echo "<h2>Uppgift 6</h2>";
echo "<p>Skapa en funktion som beräknar medelvärdet för ett godtyckligt antal tal.</p>";
function averge(int ...$lista) {
if (count($lista) === 0) {
return 0;
}
$sum = 0;
foreach ($lista as $tal) {
$sum += $tal;
}
return $sum / count($lista);
}
echo "Medelvärdet för 12, 18, 24, 30:<br>";
echo averge(12, 18, 24, 30);
echo "</div>";
?>
</div>
</body>
</html>