Tony's Function Demonstration
CODE FOLLOWS
<?php
include('includes/header.php');
/**
* FUNCTION 1: Calculation
* Takes a number and returns its square.
*/
function calculateSquare($num) {
return $num * $num;
}
/**
* FUNCTION 2: Text Manipulation
* Takes a string and returns it in "SpongeCase" (alternating caps).
*/
function formatFunnyText($text): string
{
$result = "";
$chars = str_split(strtolower($text));
foreach ($chars as $key => $char) {
$result .= ($key % 2 == 0) ? strtoupper($char) : $char;
}
return $result;
}
/**
* FUNCTION 3: HTML Change
* Takes a color name and returns a CSS-styled <span> tag.
*/
function colorizeText($text, $color): string
{
return "<span style='color: $color; font-weight: bold;'>$text</span>";
}
// Logic to handle the form submission
$output = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$userName = $_POST['userName'];
$userNum = (float)$_POST['userNum'];
// Call the functions and store results
$squaredValue = calculateSquare($userNum);
$funnyName = formatFunnyText($userName);
$styledStatus = colorizeText("Active", "green");
// Build the display string
$output .= "Hello, " . $funnyName . "!<br>";
$output .= "The square of your number is: **$squaredValue**.<br>";
$output .= "Your function processing status is: $styledStatus";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tony's Functions</title>
</head>
<body>
<h2>Tony's Function Demonstration</h2>
<form method="post" action="">
<label for="userName">Enter your name:</label><br>
<input type="text" name="userName" id="userName" required><br><br>
<label for="userNum">Enter a number to square:</label><br>
<input type="number" name="userNum" id="userNum" required><br><br>
<input type="submit" value="Run Functions">
</form>
<hr>
<?php
if ($output !== "") {
echo "<h3>Function Results:</h3>";
echo "<div>$output</div>";
}
include('includes/footer.php');
?>
</body>
</html>