What is Variable?
PHP6 introduce 64bit integer A variable is a representation of particular value. Simplay we can call this “containers” for storing information. By assinging a value to variable, you can reference the variable in other places in your script and we can change the values of variables. Choose the good and meaningful name for variable.
Rules For Variable
- Add a $ dollar sign infront of the variable name
Ex: $book_name; - PHP variable name cannot begin with a numeric character and must start with a letter or the underscore character
- PHP variable can contain numbers (0-9), letters (a-zA-z) and underscore (-)
- PHP variable name are case sensitive. $VARIABLE and $variable are two different variable
- Variable name can be any length
Variable Types
Scalar Types
- boolean
- integer
- float
- string
Compound Types
- object
- array
Special Types
- resource
- NULL
Sample Program
<?php $string_value="Hello world!"; $integer=5; $float_or_double=10.5; $boolean=true; $array=array(1,2,3); $object=new Classname(); $null_variable=NULL; ?>
Constant
- PHP constant are similar to variable and constant is case-sensitive by default.
- A constant is an identifier for a value that cannot change during the execution of a script.
- Constant dont have a dollar($) sign before their name.
- Constant are usually uppercase to show their difference from a normal variables.
- define() function is used to define a constant. define() function require the name and value of the name.
define("USERNAME","bsourcecode");
- Checks whether a given constant name exists or not
if(defined("USERNAME")) echo "Constant USERNAME is defined";
Predefined Variables
PHP provides a large number of predefined variables to all scripts. It is also called ‘Superglobals’ or automatic global variables.
- $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