Variables in PHP store values that can be changed in the script multiple times and read the values when a call upon the variable is made.

Variables starts with a dollar ($) sign plus the variable name which is case sensitive.  The scope of a variable is determined by the defined context. A variable defined inside a function is generally called a local variable with the exception that it is called a global if it is inside a function that declares it as a global variable.

Variables in PHP

Figure: Example of PHP variables

Same as constants, there are a large number of predefined variables built in PHP libraries. Some of these variables depend on the machine server. The list below includes a variety of predefined variables:

  • Superglobals — These are built-in variables that are always available in all scopes
  • $GLOBALS — References all variables available in global scope
  • $_SERVER — Server and 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 — The previous error message
  • $HTTP_RAW_POST_DATA — Raw POST data
  • $http_response_header — HTTP response headers
  • $argc — The number of arguments passed to script
  • $argv — Array of arguments passed to script

 

›› go to examples ››