0

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>" );
}
?>
3
  • Sure your for loop is right? Commented Oct 30, 2017 at 18:16
  • You want to increase the $startValue, not set it to the same value each iteration. Do for ( ..; ..; $startValue += $incValue) instead. And clean up that indentation :-) Commented Oct 30, 2017 at 18:17
  • Since you are learning: when stuck, Rubber Duck Debugging could help you identify the problem. Commented Oct 30, 2017 at 18:24

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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";

3 Comments

There is nothing wrong with modifying startValue as long as you don't assume the value is the same after the loop.
May I suggest If(!is_int($endValue/$incValue)){ echo "bad input";}
@MatsLindh: "... as long as you don't assume the value is the same...". That is why I never to it, I prefer not to modify the values I receive in case I need them later. But you are right, it can be done.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.