Print Variable

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);
?>

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.

This is an example of what I am trying to achieve:
http://www.ict.griffith.edu.au/teaching/2503ICT/Laboratories/factorise-lab/factorise.php?number=sda

I’m not entirely sure what your problem is. I can say that $error1/$error2/$error3/etc. is bad coding. You should either use a single variable ($error) or use an array ($errors[]). You shouldn’t be using 3 different variables for the same thing.

Thanks for the reply, I read what you said and I’ve tried changing my code but I am still getting a few problems with printing the error types, I can only get the missing value error to work but not the others. are you able to tell what I am doing wrong?

[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)) {
$error=“Error: Missing value”;

} elseif (!is_numeric($number)) {
$error=“Error: Nonnumeric value: $number”;

} elseif ($number < 2 || $number != strval(intval($number))) {
$error=“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);
?>

Factors

Factorisation

<?php if($number){

	echo "$number = $factors";
}
else{	
	echo "$error";
}
?></p>

<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]

Here is a live version of my site: http://dwarf.ict.griffith.edu.au/~s2850327/wp/labs/factorise/factorise.php?number= you can do a comparison with the example (mentioned in previous reply) to see what I am talking about.

I’ve resolved my problem now, I just needed to concentrate on what I was doing, thank-you very much for helping me with my query.

Sponsor our Newsletter | Privacy Policy | Terms of Service