PHP

PHP Predefined Variables & Magic Constants

Predefined Variables

  • Superglobals : Superglobals - built-in variables that are always available in all scopes. Starts with $_ + $GLOBALS
  • $GLOBALS : all variables available in global scope
  • $_SERVER: server & execution environment information
  • $_GET: HTTP GET variables
  • $_POST: HTTP POST variables
  • $_FILES: HTTP File Upload variables
  • $_REQUEST : HTTP Request variables
  • $_SESSION : Session variables
  • $_ENV : Environment variables
  • $_COOKIE: HTTP Cookies
  • $php_errormsg: Previous Error Message
  • $HTTP_RAW_POST_DATA: RAW POST data
  • $http_response_header: HTTP response headers
  • $argc: number of arguments passed to script
  • $argv: Array of arguments passed to script

Magic Constants

  • Predefined constants, but are created by various extensions so they are present only when extensions are available. (dynamic loading / compiled in)

  • Resolved at compile time (regular constants are resolved at runtime)

  • __LINE__ : current line

  • __FILE__ : full path & filename of the file with symlinks resolved.
  • __DIR__ : directory of the file.
  • __FUNCTION__ : function name
  • __CLASS__ : class name (includes namespace it was declared in)
  • __TRAIT__ : trait name (includes the namespace it was declared in)
  • __METHOD__ : class method name
  • __NAMESPACE__ : current namespace
  • ClassName::class : fully qualified class name.
Share