-4

I tried to do var_dump($_SERVER[]);

I received this error:

Cannot use [] for reading

Any hints for me?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
code-8
  • 54,650
  • 106
  • 352
  • 604

4 Answers4

4

Use

var_dump($_SERVER);

this will print all the server variables.

Somil
  • 567
  • 5
  • 13
4

Currently, every answer fails to answer the Why?.

$_SERVER[] is the syntax used to create a new sequential element. The new key will be numertical, starting from 0.

Example:

<?PHP
    $array = [];
    $array[] = 1;
    $array['a test'] = 'nope';
    $array[] = 1;

    print_r($array);

This will give the following output:

Array
(
  [0] => 1
  [a test] => nope
  [1] => 1
)

Why can't you use it for reading?

This is easy: PHP expects you to pass a value to add to the array. Since you don't give any, PHP can't read the element that is trying to create. That is like hatching an egg before there was ever a chicken.

Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42
2

You have to use var_dump($_SERVER);

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
2

Remove the [] ;) Just use var_dump($_SERVER);

Swag
  • 2,090
  • 9
  • 33
  • 63