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.











Related Posts:

  • PHP Numeric ceil <?php         $num=19.7   echo ceil($num); ?> In the above example, Initial… Read More
  • Conditional statement in PHP Conditional statement in PHP   PHP supports different types of conditional statements:- The if Statement In if Statements Output w… Read More
  • PHP OPERATORS Php operators    —Variables are simply containers for information. In order to do anything useful with them, you need Operators&n… Read More
  • Function in PHP function in PHP Function are self contained block of statement which used to perform any specific task. —A function is a block of statements t… Read More
  • PHP LOOPING PHP LOOP  —When you want the same block of code to run over and over again in a row. Instead of adding several almost equal lines in a script we… Read More