Some pre-defined
variables in php are known as superglobals variable.
Superglobals variable are always accessible and it can access from
any function, class or file.
List of
superglobals variables are given below;
·
$GLOBALS
·
$_SERVER
·
$_REQUEST
·
$_POST
·
$_GET
·
$_FILES
·
$_ENV
·
$_COOKIE
·
$_SESSION
$GLOBALS variable
$GLOBALS is a PHP super
global variable which is used to access global variables from anywhere in the
PHP script (also from within functions or methods).
Example
<?php
$x =100;
$y =50;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] +
$GLOBALS['y'];
}
addition();
echo "sum is:". $z;
?>
Output:
sum is:150
$_SERVER
Variable
$_SERVER is a super global variable which holds information
about headers, paths, and script locations.
Example
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
$_REQUEST
Variable
$_REQUEST is
used to collect data after submitting an HTML form.
Example
html>
<body>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
Name: <input
type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is
empty";
} else {
echo $name;
}
}
?>
</body>
</html>
$_POST Variable
$_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.
$_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.
Post request is widely
used to submit form that have large amount of data such as file upload, image
upload, login form, registration form etc.
Example
<html>
<body>
<form method="post" action="<?php echo
$_SERVER['PHP_SELF'];?>">
Name: <input
type="text" name="fname">
<input
type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// collect value of input field
$name = $_POST['fname'];
if (empty($name))
{
echo "Name is
empty";
} else {
echo $name;
}
}
?>
</body>
</html>
$_GET Variable
$_GET can
also be used to collect form data after submitting an HTML form with
method="get".
Example
demo.html
<html>
<body>
<form method="get" action="add.php">
Enter First Number:
<input type="number" name="num1" /><br>
Enter Second Number:
<input type="number" name="num2" /><br>
<input type="submit" name="submit" value="Add">
</form>
</body>
</html>
add.php
<?php
if(isset($_GET['submit']))
{
$number1 = $_GET['num1'];
$number2 = $_GET['num2'];
$sum = $number1+$number2;
echo "The sum of $number1 and $number2 is: ".$sum;
}
?>
<html>
<body>
<form method="get" action="add.php">
Enter First Number:
<input type="number" name="num1" /><br>
Enter Second Number:
<input type="number" name="num2" /><br>
<input type="submit" name="submit" value="Add">
</form>
</body>
</html>
add.php
<?php
if(isset($_GET['submit']))
{
$number1 = $_GET['num1'];
$number2 = $_GET['num2'];
$sum = $number1+$number2;
echo "The sum of $number1 and $number2 is: ".$sum;
}
?>
$_FILES
Variable
$_FILES contains all
the information of file. Using $_FILES
global, we can get file name, file type, file size, temp file name and errors
associated with file.
Example
<!DOCTYPE html>
<html>
<body>
<form action="indexDemo.php" method="post"
enctype="multipart/form-data">
Select File:
<input type="file"
name="fileToUpload"/>
<input
type="submit" value="Upload Image"
name="submit"/>
</form>
<?php
$target_path = "D: /";
$target_path = $target_path.basename(
$_FILES['fileToUpload']['name']);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path))
{
echo "File uploaded
successfully!";
}
else
{
echo "Sorry, file not
uploaded, please try again!";
}
?>
<body>
<html>
$_ENV Variable
$_ENV global variable
in php is a predefined reserved variable that contains an array of information
related to the environment in which php script is running.
Example
<?php
echo 'My
username is ' .$_ENV["UserName"] ;
?>
$_COOKIE Variable:
$_COOKIE is used to create cookie.
Example
<!DOCTYPE html>
<?php
$cookie_name
= "admin";
$cookie_value
= "dipak";
setcookie($cookie_name,
$cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name]))
{
echo
"Cookie named '" . $cookie_name . "' is not set";
}
else
{
echo
"Cookie '" . $cookie_name . "' is set<br>";
echo
"Value is: " . $_COOKIE[$cookie_name];
}
?>
<p
>Note: Please reload the page to see the new value of the cookie. </p>
</body>
</html>
$_SESSION Variable
$_SESSION Variable is used to create session
$_SESSION Variable
$_SESSION Variable is used to create session
Example
demo.php
<?php
session_start();
?>
<html>
<body>
<?php
$_SESSION["admin"] = "dipak";
echo "Session information is
set<br/>";
?>
<a href="result.php">Click
to get session value</a>
</body>
</html>
result.php
<?php
session_start();
?>
<html>
<body>
<?php
echo "Admin is: ".$_SESSION["admin"];
?>
</body>
</html>
I am very enjoyed for this blog. I feel strongly about it and love learning more on this topic.
ReplyDeleteIf possible, as you gain expertise, would you mind updating your blog with more information....
PHP Training Institutes
Testing Courses in Chennai
Java Course and Certification
Dot Net Course in Chennai