0

PHP: Here is what I am supposed to accomplish. And below is my code. I am definitely missing something..

Write a FOR loop using an array that prints “The product of first 10 numbers is ” followed by the product of numbers 1 through 10. Here’s a hint: do NOT begin counter at 0 or else the product will be a zero!

<?php
$numbers = 0;
$numbers = range(1 , 10);
$arrlength = count($numbers);
for ($x = 1; $x <= $arrlength; $x++) {
$numbers[$x] = $numbers + $x;
}
echo "The product of first 10 numbers is $numbers.<br>";

?>
4
  • 1
    You seem to be using $numbers for two different purposes. You should have two different variables, say $numberSum and $numbers. Commented Jul 13, 2016 at 19:48
  • 1
    I'm confused why an array is used in this question. Commented Jul 13, 2016 at 19:50
  • 1
    Sounds like an assignment. You wouldn't need an array for this task. If you really need an array you could use array_sum on it to calculate the sum. php.net/manual/en/function.array-sum.php Commented Jul 13, 2016 at 19:51
  • @cmorrissey I'm also confused why the result is being summed and not multiplied since the exercise asks for a product. Commented Jul 13, 2016 at 19:51

2 Answers 2

4

Essentially what you're doing is creating a factorial function for a limited case. The problems with your implementation are that you seemed to re-use the $numbers variable when it should be one variable to hold the product and one for the array 1-10. The second issue is in your loop where you loop from 1-10 but what you probably really want to do is loop from 0-9 which are the array indices so that you can get the values from the array like this:

<?php
$prod = 1;
$numbers = range(1 , 10);
$arrlength = count($numbers);
for ($x = 0; $x < $arrlength; $x++) {
     $prod *= $numbers[$x];
}
echo "The product of first 10 numbers is $prod.<br>";
?>

The output of which will be:

The product of first 10 numbers is 3628800.
Sign up to request clarification or add additional context in comments.

3 Comments

yes you are correct, it is product and not sum. Not sure where I got that from.
If I just want to skip the array and do this:<?php $total = 1; for ($x = 0; $x <= 10; $x++) { $total *= $total[$x]; } echo "The product of first 10 numbers is $total.<br>"; ?> why am I getting a 0 instrad of 3628800?
What's $total[$x] supposed to refer to if there's no array? You probably meant <?php $total = 1; for ($x = 1; $x <= 10; $x++) { $total *= $x; } echo "The product of first 10 numbers is $total.<br>"; ?>
0

Please note I have not tested this code, but I'm sure it should work.

<?php
  $numbers = range(1, 10);
  $product = 1;

  for ($i = 0; $i < count($numbers); $i++) {
    $product *= $numbers[$i];
  }

  echo 'The product of the first 10 numbers is ' . $product . '<br />';
?>

1 Comment

I just noticed you want the product not the sum :/ Just fixed that.

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.