user1667307
user1667307

Reputation: 2071

Unable to get request parameters in php

I am using the following code to get data from $_REQUEST in php:

for($i=0;$i<intval($q);$i++)
    {
       $construct="'".$i."'";
       echo $construct;
       $p=$_REQUEST[$construct];
       echo $p;

    }

where '0','1',... so on have values. But for some reason it does not work. However if I replace it by $_REQUEST['0'] it seems to print the value fine. Any ideas what I am doing wrong?

Upvotes: 0

Views: 405

Answers (2)

poplitea
poplitea

Reputation: 3727

Remove the quotes around $i:

for($i=0;$i<intval($q);$i++)
{
   $construct=$i;
   echo $construct;
   $p=$_REQUEST[$construct];
   echo $p;

}

This is because the quotes are not part of the array indexes.

Besides, if you want to debug, you could write out the entire contents of the $_REQUEST-variable like this:

print "<pre>";
print_r($_REQUEST);
print "</pre>";

EDIT:

$a[$b] is the same whether $b=0 or $b='0' or $b="0", but not when $b="'0'".

Upvotes: 1

doublesharp
doublesharp

Reputation: 27599

Your $construct variable's value contains single quotes, which are not necessary. When you write it out as '0' you are indicating an actual value of 0, whereas "'0'" indicates a value of '0'. Your code should read:

for($i=0;$i<intval($q);$i++)
{
   $construct= strval($i);
   echo $construct;
   $p=$_REQUEST[$construct];
   echo $p;
}

or more simply:

for ($i=0; $i<intval($q); $i++) {
   echo $_REQUEST[strval($i)];
}

Upvotes: 1

Related Questions