PHP Lab 6
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 43 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 67 KiB |
Binary file not shown.
@@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
// Get and print age, print error if empty
|
||||||
|
|
||||||
|
if (!isset($_POST["ageInput"]) || $_POST["ageInput"] === "") {
|
||||||
|
print("Please enter a valid age above or equal to 0<br>");
|
||||||
|
} else {
|
||||||
|
$userAge = $_POST["ageInput"];
|
||||||
|
$birthYear = date("Y") - $userAge;
|
||||||
|
if ($userAge < 0) {
|
||||||
|
print("Please enter a valid age above or equal to 0");
|
||||||
|
} else {
|
||||||
|
print("Your age is " . $userAge . "<br>");
|
||||||
|
|
||||||
|
// Two ways to determine even-ness
|
||||||
|
$isEven = ($userAge % 2 === 0) ? "Your age is even<br>" : "Your age is odd<br>";
|
||||||
|
print($isEven);
|
||||||
|
|
||||||
|
// OR
|
||||||
|
// if ($userAge % 2 === 0) {
|
||||||
|
// print("Your name is even<br>");
|
||||||
|
// } else {
|
||||||
|
// print("Your name is odd<br>");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Print age "category"
|
||||||
|
if ($userAge <= 12) {
|
||||||
|
print("You are a child<br>");
|
||||||
|
} else if ($userAge > 12 && $userAge <= 17) {
|
||||||
|
print("You are a teenager<br>");
|
||||||
|
} else if ($userAge > 17 && $userAge <= 54) {
|
||||||
|
print("You are an adult<br>");
|
||||||
|
} else {
|
||||||
|
print("You are a senior<br>");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print significant events with switch statement
|
||||||
|
switch ($birthYear) {
|
||||||
|
case 1969:
|
||||||
|
print("You were born the same year as the moon landing<br>");
|
||||||
|
break;
|
||||||
|
case 1983:
|
||||||
|
print("You were born the same year as the creation of the internet<br>");
|
||||||
|
break;
|
||||||
|
case 1986:
|
||||||
|
print("You were born the same year as the Challenger explosion<br>");
|
||||||
|
break;
|
||||||
|
case 2000:
|
||||||
|
print("You were born the same year as Y2K<br>");
|
||||||
|
break;
|
||||||
|
case 2020:
|
||||||
|
print("You were born the same year as the COVID-19 pandemic<br>");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
print("You were not born in any significant year<br>");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print age number of hearts
|
||||||
|
for ($i = 0; $i < $userAge; $i++) {
|
||||||
|
print("♥");
|
||||||
|
}
|
||||||
|
|
||||||
|
print("<br>");
|
||||||
|
|
||||||
|
// Print 100 - age number of stars
|
||||||
|
for ($i = 0; $i < (100 - $userAge); $i++) {
|
||||||
|
print("☆");
|
||||||
|
}
|
||||||
|
|
||||||
|
print("<br>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>PHP Lab 6</title>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Age Result Generator</h1>
|
||||||
|
<form method="post">
|
||||||
|
<label for="ageInput">Please enter your age:</label>
|
||||||
|
<input name="ageInput" type="number"><br>
|
||||||
|
<button type="submit">Submit</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user