PHP Variables And Constant
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
- 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
<?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; ?>
- 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.
Checks whether a given constant name exists or not
if(defined("USERNAME")) echo "Constant USERNAME is defined";
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