I have a bit of a confusing question that’s been frustrating me, I want to know why I am not able to print a variable correctly. Here is a sample of the code, it’s meant to do a calculation but if an invalid input is entered (nonempty, numeric and between 2 and PHP_MAX_INT = 2^31-1) it prints a warning message.
[php]<?php
/*
- Script to print the prime factors of a single positive integer
- sent from a form.
- BAD STYLE: Does not use templates.
*/
include “includes/defs.php”;
Set $number to the value entered in the form.
$number = $_GET[‘number’];
Check $number is nonempty, numeric and between 2 and PHP_MAX_INT = 2^31-1.
(PHP makes it difficult to do this naturally; see the manual.)
if (empty($number)) {
$error1=“Error: Missing value”;
} else if (!is_numeric($number)) {
$error2=“Error: Nonnumeric value: $number”;
} else if ($number < 2 || $number != strval(intval($number))) {
$error3=“Error: Invalid number: $number”;
}
Set $factors to the array of factors of $number.
$factors = factors($number);
Set $factors to a single dot-separated string of numbers in the array.
$factors = join(" . ", $factors);
?>
Factorisation
<?php if($error1){ echo "$error1"; } if($error2){ echo "$error2"; } if($error3){ echo "$error3"; } if($number){ echo "$number = $factors"; } ?>
<h2>Another factorisation</h2>
<form method="get" action="factorise.php">
<p>Number to factorise: <input type="text" name="number" value="<?= $number ?>" >
<p><input type="submit" value="Factorise it!">
</form>
[/php]
If I am not making much sense please post a reply.