This commit is contained in:
jakub
2025-05-23 20:04:55 +02:00
parent 0fb58def1a
commit 37f309f424
7 changed files with 396 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
<!doctype html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>T54.1 - zastosowanie funkcji</title>
<link rel="stylesheet" href="../style.css">
</head>
<body>
<header>
<h1>Zadanie T54.1</h1>
<h2>Autor: Jakub Grzegorczyk</h2>
</header>
<div class="box">
<p>
Zadanie T541 <br><br>
Napisz funkcję, która dla podanej liczby zwraca (return) jej wartość bezwzględną i zastosuj funkcję do wyświetlenia wyniku. Do obliczenia wartości bezwzględnej użyj instrukcji warunkowej (wariant 1) i operatora warunkowego "?" (wariant 2).
</p>
</div>
<div class="box">
<form action="index.php" method="post">
<label for="x">Wprowadź liczbę: </label><br>
<input type="number" name="x" id="x"><br>
<button type="submit">Wyświetl</button>
</form>
</div>
<div class="box">
<?php
function wartoscBezwzglednaIf($liczba) {
if ($liczba < 0) {
return -$liczba;
}
return $liczba;
}
function wartoscBezwzglednaTernary($liczba) {
return ($liczba < 0) ? -$liczba : $liczba;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<div class='box'>";
$liczba = (int)htmlspecialchars($_POST['x']);
echo "Wariant 1: " . wartoscBezwzglednaIf($liczba) . "<br>";
echo "Wariant 2: " . wartoscBezwzglednaTernary($liczba) . "<br>";
echo "</div>";
}
?>
</div>
</body>
</html>