17780

Is there any differents between $GLOBALS[“test”] and global $test?

Question:

can any one please let me know the basic differences between

$GLOBALS["test"] and global $test

and, will it make sense that, if i use $GLOBALS["test"] instead of $_SESSION['test']?

Answer1:

<blockquote>

and, will it make sense that, if i use $GLOBALS["test"] instead of $_SESSION['test']?

</blockquote>

No, <strong><a href="http://www.php.net/manual/en/intro.session.php" rel="nofollow">session</a></strong> is different thing from a variable that is available globally.

<strong>$GLOBALS</strong>

<blockquote>

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

</blockquote>

<a href="http://php.net/manual/en/reserved.variables.globals.php" rel="nofollow">http://php.net/manual/en/reserved.variables.globals.php</a>

<strong>Explanation:</strong>

$GLOBALS is an associative array available throughout your script, there is no need to use global $test

<blockquote>

Note: This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

</blockquote>

Answer2:

There is no difference between $GLOBALS["test"] and global $test. Both are pure evil and shouldn't be used.

<h2>Why are they evil?</h2> <ol><li>Suddenly your code becomes depended on some outer environment, its portability falls head over heels. It requires some variable defined somewhere, nobody know where, with some value, nobody know what's the proper value.</li> <li>Imagine that $test is supposed to store an information about something, let's say: number of balls. Everything is fine until there is such a variable and it store what it's suppose to store. However what happen if you decide to delete that variable or use it for other purpose? Bah, fatal errors pop out of nowhere! You don't know what's going on, everything worked fine, you just change a variable's value and everything is falling apart.</li> </ol>

Recommend

  • SWIFT uiviewcontroller init
  • Saving an array globally
  • Does as-if rule prevent compiler reordering of accesses to global/member variables?
  • Symfony2 - Composer class loader instance in controller
  • DELETE FROM … reporting syntax error at or near “.”
  • SQL Server 2005, Caches and all that jazz
  • Process.PrivateMemorySize64 returning committed memory instead of private
  • php notice undefined index http_host in $GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST'
  • Compress a file with GZipStream while maintaining its meta-data
  • How do I remove all but some records based on a threshold?
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • How can I include If-None-Match header in HttpRequestMessage
  • Suppressing passwd when calling sqlplus from shell script
  • Security issues with PHP's Readfile method
  • Array with custom indexes in Ionic2
  • Parse a date string in a specific locale (not timezone!)
  • Installed module is empty
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Display issues when we change from one jquery mobile page to another in firefox
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Check if a string to interpolate provides expected placeholders
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Validaiting emails with Net.Mail MailAddress
  • Javascript + PHP Encryption with pidCrypt
  • When should I choose bucket sort over other sorting algorithms?
  • Upload files with Ajax and Jquery
  • How do you troubleshoot character encoding problems?
  • Do I've to free mysql result after storing it?
  • bootstrap to use multiple ng-app
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to get icons for entities from eclipse?
  • A cron job substitute?
  • json Serialization in asp
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Understanding cpu registers
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • Unable to use reactive element in my shiny app