Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable variable Superglobals

Tags:

php

In PHP ...

var_dump(${'_GET'});   // array(0) { } - #1

$var = '_GET';
var_dump(${$var});     // array(0) { } - #2

function test() {
  var_dump(${'_GET'}); // array(0) { } - #3
  $var = '_GET';
  var_dump(${$var});   // NULL - #4
}
test();

What happen?

like image 383
Steven Scanlon Avatar asked Sep 26 '13 09:09

Steven Scanlon


People also ask

What is PHP Superglobals variables?

Some predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

What is use of $_ GET [] and $_ request [] variables explain with example?

PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form. The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag.

Is $_ POST global variable?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.

What is the difference between global and Superglobals in PHP?

E.g. if you're about to access a variable inside a function that's defined outside you'll need to use the global keyword to make it accessible in the function. $GLOBALS is a superglobal array. Superglobal simply means that it is available in all scopes throughout a script without the need of using the global keyword.


1 Answers

It's not a bug, it's a feature:

Warning

Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.

like image 70
Jon Avatar answered Sep 30 '22 11:09

Jon