<?php
include('includes/header.php');
// code
error_reporting(E_ALL); // set error reporting to all
echo('<h1>Isaac\'s Functions</h1>');
// 1. Mathematical Operation Function
// Takes a value and performs a calculation that divides the input number by 2.
function divideByTwo($number) {
if (!is_numeric($number)) {
return 0;
}
return $number / 2;
}
// 2. Text Manipulation Function
// Takes text, bolds the letters, and appends a results string.
function formatBoldText($text) {
if (empty($text)) {
return "<b>[Empty Value]</b>";
}
return "<b>" . $text . "</b> ";
}
// 3. HTML Manipulation Function
// Returns an HTML change like making the text color green.
function applyGreenStyle($text) {
if (empty($text)) {
return "<span style='color: green;'>[Empty Value]</span>";
}
return "<span style='color: green; font-weight: bold;'>" . $text . "</span>";
}
$step = isset($_POST['step']) ? $_POST['step'] : 0;
// Gather inputs for operations
$calc_number = isset($_POST['calc_number']) ? $_POST['calc_number'] : '';
$manip_text = isset($_POST['manip_text']) ? $_POST['manip_text'] : '';
$green_text = isset($_POST['green_text']) ? $_POST['green_text'] : '';
// filter all input taken from the Browser
$step = htmlentities($step);
$calc_number = htmlentities($calc_number);
$manip_text = htmlentities($manip_text);
$green_text = htmlentities($green_text);
if ($step == 0) // first time into this form
{
?>
<form action="my-function.php" method="POST" class="pad">
<h3>1. Mathematical Function</h3>
<label for="calc_number">Number</label>
<input type="number" size="36" id="calc_number" name="calc_number" value="42" step="any">
<br><br>
<hr>
<h3>2. Text Manipulation Function</h3>
<label for="manip_text">Input text to make Bold</label>
<input type="text" size="36" id="manip_text" name="manip_text" value="Hello World">
<br><br>
<hr>
<h3>3. HTML Manipulation Function</h3>
<label for="green_text">Input text to turn Green</label>
<input type="text" size="36" id="green_text" name="green_text" value="Make me green">
<br><br>
<input type="hidden" name="step" value="1">
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
else // else show what the form gathered and pass them into the functions
{
echo('<h2>Function Processing Results</h2>');
// 1. Run the mathematical operation function
$calcResult = divideByTwo($calc_number);
echo('<b>1. Number divided by 2</b><br>');
echo('Original Value: ' . $calc_number . ' | Result Divided by Two: ' . $calcResult . '<br><br><hr>');
// 2. Run the text manipulation function
$textResult = formatBoldText($manip_text);
echo('<b>2. formatBoldText() Text Manipulation Output:</b><br>');
echo($textResult . '<br><br><hr>');
// 3. Run the HTML manipulation function
$htmlResult = applyGreenStyle($green_text);
echo('<b>3. applyGreenStyle() HTML Transformation Output:</b><br>');
echo($htmlResult . '<br><br><hr>');
// provide a form to try again
?>
<form action="my-function.php" method="POST">
<input type="hidden" name="step" value="0">
<input type="submit" name="submit" value="Try Again">
</form>
<?php
}
include('includes/footer.php');
?>