Thursday 10 December 2015

$var & $$var and php super global variables

Difference Between $var and $$var in Php
$$var uses the value of the variable whose name is the value of $var.
It means $$var is known as refrence variable where as $var is normal variable.
It allows you to have a “variable’s variable” – the program can create the variable name the same way it can create any other string. .
Example:
<?php
 $name="Rajeev";
$$name="Sanjeev";
echo $name."<br/>";
echo $$name."<br/>";
echo $Rajeev;
?>

Output : Rajeev Sanjeev Sanjeev 

In the above example
$name is just a variable with string value=”Rajeev”.
$$name is reference variable . $$name uses the value of the variable whose name is the value of $name.
echo $name print the value:
rajeev
echo $$name print the value:Sanjeev \\ value of this($name) variable is act as reference of second variable($$name).
echo $rajeev print the value :Sanjeev \\ Here $rajeev is also act as reference variable.
Example-2:
<?php
$x = "100";
$$x = 200;
echo $x."<br/>";
echo $$x."<br/>";
echo "$100";
?>
Output : 100 200 200 

php super global variables
 PHP super global variable is used to access global variables from anywhere in the PHP script.
PHP Super global variables is accessible inside the same page that defines it, as well as outside the page.
while local variable’s scope is within the page that defines it.
The PHP super global variables are :
$_GET[ ] : It is used to collect value from a form(HTML script) sent with method=’get’.
information sent from a form with the method=’get’ is visible to everyone(it display on the browser URL bar).
$_POST[ ] : It is used to collect value in a form with method=”post”>. Information sent from a form is invisible to others.(can check on address bar)
$_REQUEST[ ] : This can be used to collect data with both post and get method.
$_FILES[ ] : It can be used to upload files from a client computer/system to a server.
$_SESSION[ ] : A session variable is used to store information about a single user, and are available to all pages within one application.
$_COOKIE[ ] : A cookie is used to identify a user. cookie is a small file that the server embedded on user computer.
$_SERVER[ ] : $_SERVER holds information about headers, paths, and script locations.