0

I have the PHP code below:

<?php

    $length = $_GET["length"];
    $maxValue = $_GET["maxValue"];
    $distribution = array();

    for($j = 0; $j < $maxValue; $j++) {
        $distribution[j] = 5;
    }

    $x = 0;
    $x++;

    for($j = 0; $j < $maxValue; $j++) {
        echo $distribution[j] , " ";
    }

    echo $x;

?>

$x starts as 0 and is incremented by 1. However, just below $x is incremented, I am also incrementing the first element of the "distribution" array - $distribution[0]. And it's not working. It worked fine when I was initializing the elements (set them to 5).

Any ideas on why it might now be working? I am probably referencing the array element wrong. But this seems inconsistent.

2
  • 2
    $distribution[j] -> $distribution[$j] Dollar sign missing Commented Aug 17, 2015 at 16:08
  • Also I don't really see what your question is. Means what your current output is and what you expected to get. Commented Aug 17, 2015 at 16:11

1 Answer 1

3

When you say $distribution[j] -> php doesn't understand the j as a variable - but rather as an undefined constant

It looks like you are trying to say $distribution[$j] - which is partially -why your increments aren't working - -

The other reason would be that you aren't ever calling $distribution[$j]++ --- so there is no incrementation happening...

Sign up to request clarification or add additional context in comments.

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.