• PHP Help PHP Coding
  • Is "$Page = $_POST['Page']" right way to initialize array with register_globals off?

My web hosting has got register_globals turned off. In order to activate or initialize the array Page[] below I am putting "$Page = $_POST['Page'];" on Page2.php.

My question is: Is the $_POST['Page'] method the right way to initialize an array?

I know that the $_POST[''] method is the right way (or one of the right ways) for a variable. I just want to make sure that I'm doing it the right way for an array variable.

// Page1.php
<form method="post" action="Page2.php>
<input type="checkbox" name="Page[]" value="0">
<input type="checkbox" name="Page[]" value="1">
<input type="checkbox" name="Page[]" value="2">
<input type="checkbox" name="Page[]" value="3">
<input type="checkbox" name="Page[]" value="4">
</form>

The code below processes the input data on Page1.php above:

// Page2.php
// Is this the right way to declare the variable.
$Page = $_POST['Page'];

// I'm using a foreach loop here to process the Page[] array values.

Thanks.

    This might be better, to account for the possible non-existence of the incoming variable, or it somehow not being an array:

    $Page = (isset($_POST['Page']) && is_array($_POST['Page'])) ? $_POST['Page'] : array();

      And for a regular POST variable, you'd simply get rid of the is_array() check and initialize it to an empty string rather than an empty array.

        laserlight, bradgrafelman;

        Thanks for the help. I appreciate the recommendations.

          Write a Reply...