What is PHP Variables
Variables are "containers" for storing information. PHP Global variable PHP Session variable PHP Static variable PHP Local variable
Variables play an important role in PHP, as they are places for holding values. In PHP, there is no need to declare variables. Variable can hold eight different data types:
PHP is a weakly typed language. This means that variable type varies depending on what is stored in the variable at the time. For example, if we have a variable $a, when $a = 0, $a is an integer type variable.If later we set $a = “New”, then $a becomes a string type variable.
Php Variables Name
A variable name always starts with a $, followed by a letter or an underscore.
- The rest of the variable name can be a letter, a number or an underscore.
- For example, $dog is a valid variable name, while @dog is not (@dog does not
- start with a $).
- Variables in PHP are case-sensitive. For example, $Employee and $employee
- are two different variables.
PHP Variables Scope
In most cases, variables are local in scope. This means that variables declared within a function cannot be accessed outside of the function, and variables declared outside of a function cannot be access within the function. To make a variable global, it must either be declared as global specifically, or it must be accessed using the $GLOBALS array.
This is predefine variable and these variables are automatically available to any PHP program. There is no need to declare and define these variable in the program. These are following type of SuperGlobal Variable.
- $_GET When we send data through form tag by get method and send data through querystring , then we reterive data by this superglobal variable .
- $_POST When we send data through form tag by post method then we reterive data by this superglobal variable.
- $_COOKIE We reterive cookies data through this superglobal variable.
- $_SESSION We reterive session data through this superglobal variable.
- $_FILES When we use file browse html control then we reterive file name,type and size by this superglobal variable.
- $_ENV We reterive variables available from the server environment.
- $_REQUEST This superglobal contains the data that sent by get method, post method and query string.
- $_SERVER This is an array variable that contain information about webserver such file path,etc.