23.05
This commit is contained in:
52
Tematy/T54 - zastosowanie funkcji/T54.1/index.php
Normal file
52
Tematy/T54 - zastosowanie funkcji/T54.1/index.php
Normal 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 tę 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>
|
||||
Reference in New Issue
Block a user