Källkod
Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.
webbserverprogrammering/exercises/repetition/
4 filer
repetition.php
123 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
<?php
error_reporting(-1); // Report all type of errors
ini_set('display_errors', 1); // Display all errors
ini_set('output_buffering', 0); // Do not buffer outputs, write directly
session_start();
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<title>Repetition</title>
<meta charset="utf-8">
</head>
<body>
<h1>Repetition</h1>
<?php
$array = [];
$value = 1;
for ($i = 0; $i < 10; $i++) {
$array[] = $value;
$value *= 2;
}
foreach ($array as $element) {
echo "$element<br>";
}
echo "<br>Medelvärde: " . (array_sum($array) / count($array));
echo "<hr><br>";
function getBiggestNr($nr1, $nr2, $nr3) {
return max($nr1, $nr2, $nr3);
}
$biggest = getBiggestNr(4, 2, 9);
echo $biggest;
echo "<br><hr><br>";
function getAverage($array) {
return array_sum($array) / count($array);
}
if (isset($_POST["value"]) && $_POST["value"] !== "") {
$array2 = [];
for ($i = 0; $i < $_POST["value"]; $i++) {
$array2[] = rand(1, 10);
}
echo "<strong>Medelvärde för array2 (X antal slumpade, slumpat 1-10)</strong>:<br>" . round(getAverage($array2), 3);
}
?>
<form method="post" action="">
<input type="number" name="value" placeholder="hur många slumpningar"><br>
<input type="submit" name="submitted" value="Beräkna">
</form>
<?php
echo "<br><hr><br>";
?>
<form method="post" action="">
<input type="text" name="inputName" placeholder="Namn"><br>
<br>Maträtter
<select name="food">
<option value="pasta bolognese">Pasta bolognese</option>
<option value="kebab">Kebab</option>
<option value="janssons frestelse">Janssons frestelse</option>
</select>
<br>
<input type="submit" name="submitted">
</form>
<?php
if (isset($_POST["inputName"])) {
echo "<h1>Hej {$_POST['inputName']}! Du gillar {$_POST['food']}!</h1>";
}
echo "<br><hr><br>";
?>
<form method="post" action="">
<br>
<?php
if (isset($_POST["guessNr"])) {
if (isset($_POST["myName"])) {
$_SESSION["repetitionRandName"] = $_POST["myName"];
}
$_SESSION["repetitionRandTries"]++;
if ($_POST["guessNr"] == $_SESSION["repetitionRandNr"]) {
echo "Hej {$_SESSION['repetitionRandName']}! Rätt gissat! Det tog {$_SESSION['repetitionRandTries']} försök!<br>";
$_SESSION["repetitionRandTries"] = 0;
$_SESSION["repetitionRandNr"] = rand(1, 15);
}
else {
echo "Hej {$_SESSION['repetitionRandName']}! Fel gissat, du har nu gissat {$_SESSION['repetitionRandTries']} gånger!<br>";
}
}
else {
$_SESSION["repetitionRandTries"] = 0;
$_SESSION["repetitionRandNr"] = rand(1, 15);
echo '<input type="text" name="myName" placeholder="Namn"><br>';
}
?>
<input type="number" name="guessNr" placeholder="Tal" min="1" max="15" width="100"><br>
<input type="submit" name="submitted" value="Gissa tal (1-15)">
</form>
</body>
</html>