Reputation: 1
I am trying to make a program where users can input a starting number, ending number, and the increment amount. The program should confirm each value is numeric and then square it and return it and then increase by the increment amount. The program does 1 operation and then stops if I put in an end value of less than 10 but doesnt run at all if its more than 10. I am a student and my prof. is gone for the next two weeks. Any feedback would be helpful, thanks.
For example
Start Value: 5
End Value: 20
Increment: 5
Your results should be:
5 squared is 25
10 squared is 100
15 squared is 225
20 squared is 400
<?php
$endValue = $_POST['endValue'];
$startValue = $_POST['startValue'];
$incValue = $_POST['incValue'];
if (is_numeric($endValue))
{
for ($startValue = $startValue; $startValue <= $endValue; $startValue = $incValue * $incValue);
{
print("<p> testing $startValue </p>");
}
}
else
{
print("<p> Bad input - use a number </p>");
print("<p><a href=\"squared.html\"> return </a></p>" );
}
?>
Upvotes: 0
Views: 282
Reputation: 3832
The semi-colon after the for-loop is problematic. Also, you need to be more careful about using $_POST data. Consider it tainted until you validate it. Your for-loop doesn't need an initializer since you already did that outside the loop preceding it. Your increment section needs correction as well to get the results you seek.
So, here's a working example to give you a general guideline:
<?php
$endValue = 0;
$startValue = 0;
$incValue = 0;
// simulating a $_POST for testing ...:
$_POST['endValue'] = 20;
$_POST['startValue'] = 5;
$_POST['incValue'] = 5;
if ( is_int( $_POST['endValue'] ) ){
$endValue = trim( $_POST['endValue'] );
}
if ( is_int( $_POST['startValue'] ) ) {
$startValue = trim( $_POST['startValue'] );
}
if ( is_int( $_POST['incValue'] ) ) {
$incValue = trim( $_POST['incValue'] );
}
if ($endValue == 0 || $startValue == 0 || $incValue == 0) {
trigger_error("\nBad Input...ending now.");
}
for (; $startValue <= $endValue; $startValue += $incValue )
{
print("\n<p> testing $startValue squared = " . $startValue * $startValue . "</p>");
}
print("\n<p><a href=\"squared.html\"> return </a></p>" );
See live code
Note: you may optionally use instead the pow() instead of multiplying $startValue * itself.
Upvotes: 0
Reputation: 3879
Use pow function for square the number. Also you need to increment the number by 5 not multiple it.
$endValue = 20;
$startValue = 5;
$incValue = 5;
If (is_numeric($endValue))
{
for ($startValue = $startValue; $startValue <= $endValue; $startValue += $incValue)
{
echo "$startValue squared is " . pow($startValue, 2) . "\n";
}
}
else
{
print("<p> Bad input - use a number </p>");
print("<p><a href=\"squared.html\"> return </a></p>" );
}
Out put:
5 squared is 25
10 squared is 100
15 squared is 225
20 squared is 400
Upvotes: 2
Reputation: 8611
In your for loop define another variable as the loop index and change that one. Increment $i by the value of $incValue. Like so:
echo "<pre>\n";
$endValue = 20;
$startValue = 5;
$incValue = 5;
If (is_numeric($endValue))
{
for ($i = $startValue; $i <= $endValue; $i += $incValue)
{
echo "$i squared is " . $i**2 . "\n";
}
}
else
{
print("<p> Bad input - use a number </p>");
print("<p><a href=\"squared.html\"> return </a></p>" );
}
echo "</pre>\n";
Upvotes: 0