What is Variables in PHP?
A variable is just a "container" area where we store the information (Like string or integer or or arrays value). PHP automatically convert Variables data types according its value.
<?php
$text = "strings variable";
$number = 5;
?>
Note:
- Variable names should be case-sensitive, and In PHP variables are denoted with ($) dollar.
- A variable name can not start with a number (means only contain alpha-numeric characters and underscores).
- A variable name can not contain spaces.
What is Constants Variable in PHP?
A constant Variable can't change at execution time.
Constant Variable is defined using define() function,which accepts two arguments: the name of the constant, and its value.
<?php
// Defining constants
define("SITE_URL", "https://phplanguagecode.blogspot.com/");
// Using constants
echo "Visit my website ".SITE_URL;
?>
if you use SITE_URL any where then its show full url.
What is Variable References?
PHP also allows to create aliases or reference for variables, means a aliases is a variable, that assigned to or refer to the same information as another variable.
<?php
$a = 'nice day!';
$b = &$a;
$b = "Have a $b";
?>
Super global Variables.
PHP has several predefined variables these are called Super Globals. The PHP superglobal variables are:
- $GLOBALS
- $_SESSION
- $_SERVER
- $_COOKIE
- $_GET
- $_POST
- $_REQUEST
- $_FILES
- $_ENV
PHP $_SERVER
which holds information about headers, paths, and script locations.
<?php
echo $_SERVER['SERVER_NAME'];
echo $_SERVER['PHP_SELF'];
echo $_SERVER['HTTP_HOST'];
?>
PHP $_REQUEST
collect data after submitting a form by GET or POST Method.
PHP $GLOBALS
for access global variables from anywhere in the PHP . it stores all variables in an array called $GLOBALS[index]. The index holds the name of the variable. example
<?php
$a = 5;
$b = 7;
function addition() {
$GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b'];
}
addition();
echo $c;
?>