0

1.php

<?php

header( 'Location: 4.php?$x=1&y=2&z=3' );

?>  

sends the value of x ,y ,z

4.php

<?php
 print '<pre>';
$a= $_GET ;
echo $a[x];
print '</pre>';

?>

when we call 1.php it is redirected to 4.php
it displays the value of x correct but it gives error

Notice: Use of undefined constant x - assumed 'x' in C:\wamp\www\4.php on line 6

why it gives error ?

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 1
    possible duplicate of [What does the PHP error message "Notice: Use of undefined constant" mean?](http://stackoverflow.com/questions/2941169/what-does-the-php-error-message-notice-use-of-undefined-constant-mean) – mario Dec 29 '12 at 04:34

2 Answers2

3

Common bug again...

echo $a[x];

should be

echo $a['$x'];

in echo $a[x];, x is treated as (so called) "bare string", and PHP will look for a constant named x, which does not exist.

On the other hand, you need to get the $x key in the $_GET superglobal, which is populated by PHP from your URL.

luiges90
  • 4,493
  • 2
  • 28
  • 43
1

You need this instead:

echo $a['$x']

Note that you're passing in $x in the query string. Make sure you use the appropriate string key of $_GET, or $a in your case.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 2
    @user1935863 if it works for you.Accept this answer as the correct answer.Then it will credit to Brad's reputation.:) – vinu Dec 29 '12 at 04:55