$ ./webserver_programming_1

Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg/exercises/

ovn_fr.php
ovn_funk.php
ovn_gr_1-5.php
style/

ovn_funk.php

133 lines UTF-8 Windows (CRLF)
<?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>