Judul : PHP Global Variables - Superglobals
link : PHP Global Variables - Superglobals
PHP Global Variables - Superglobals
PHP Global Variables - Superglobals:
Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.
The PHP superglobal variables are:
This TOpic will explain some of the superglobals, and the rest will be explained in later chapters.
PHP $GLOBALS
$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).
PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.
The example below shows how to use the super global variable $GLOBALS:
Example :
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
output:
100
In the example above, since z is a variable present within the $GLOBALS array, it is also accessible from outside the function!
PHP $_SERVER :
$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.
The example below shows how to use some of the elements in $_SERVER:
Example :
save file name as server.php :
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
// echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
output:
/phpvariable/server.php
localhost
localhost
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
/phpvariable/server.php
PHP $_REQUEST :
PHP $_REQUEST is used to collect data after submitting an HTML form.
The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag. In this example, we point to this file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_REQUEST to collect the value of the input field:
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 = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
PHP $_POST :
PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.
The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag. In this example, we point to the file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_POST to collect the value of the input field:
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>
PHP $_GET :
PHP $_GET can also be used to collect form data after submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with parameters:
<html>
<body>
<a href="test_get.php?subject=PHP&web=phpdevelopmenttricks.blogspot.com">Test $GET</a>
</body>
</html>
When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you can then access their values in "test_get.php" with $_GET.
The example below shows the code in "test_get.php":
Example :
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>
Example:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "saurabh";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql ="select * from hotel ";
$result=mysqli_query($conn, $sql) or die(mysqli_error());
echo '<table><tr> <th> hotel name</th> <th>profiel </th><th>address </th> </tr>';
while($row=mysqli_fetch_array($result))
{
?>
<tr> <td>
<a href="ankit.php?id=<?php echo $row['id']; ?>"> <?php echo $row['hotelname']; ?></a> </td>
<td>
<a href="ankit.php?id=<?php echo $row['id']; ?>"> <?php echo $row['hotelprofile']; ?></a>
</td>
<td>
<a href="ankit.php?id=<?php echo $row['id']; ?>"> <?php echo $row['address']; ?></a>
</td>
</tr>
<?php
}
echo '</table>';
?>
<?php
//$id=$_GET['id'];
if(isset($_GET['id']))
{
$id=$_GET['id'];
echo $id;
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "saurabh";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql="select * from hotel where id='$id'";
$result=mysqli_query($conn, $sql) or die(mysqli_error());
echo '<table><tr> <th> hotel name</th> <th>profiel </th><th>address </th> </tr>';
while($row=mysqli_fetch_array($result))
{
echo $row['hotelname'];
echo $row['hotelprofile'];
echo $row['address'];
}
}
?>
Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.
The PHP superglobal variables are:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
This TOpic will explain some of the superglobals, and the rest will be explained in later chapters.
PHP $GLOBALS
$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).
PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.
The example below shows how to use the super global variable $GLOBALS:
Example :
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
output:
100
In the example above, since z is a variable present within the $GLOBALS array, it is also accessible from outside the function!
PHP $_SERVER :
$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.
The example below shows how to use some of the elements in $_SERVER:
Example :
save file name as server.php :
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
// echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
output:
/phpvariable/server.php
localhost
localhost
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
/phpvariable/server.php
PHP $_REQUEST :
PHP $_REQUEST is used to collect data after submitting an HTML form.
The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag. In this example, we point to this file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_REQUEST to collect the value of the input field:
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 = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
PHP $_POST :
PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.
The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag. In this example, we point to the file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_POST to collect the value of the input field:
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>
PHP $_GET :
PHP $_GET can also be used to collect form data after submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with parameters:
<html>
<body>
<a href="test_get.php?subject=PHP&web=phpdevelopmenttricks.blogspot.com">Test $GET</a>
</body>
</html>
When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.php", and you can then access their values in "test_get.php" with $_GET.
The example below shows the code in "test_get.php":
Example :
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>
Example:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "saurabh";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql ="select * from hotel ";
$result=mysqli_query($conn, $sql) or die(mysqli_error());
echo '<table><tr> <th> hotel name</th> <th>profiel </th><th>address </th> </tr>';
while($row=mysqli_fetch_array($result))
{
?>
<tr> <td>
<a href="ankit.php?id=<?php echo $row['id']; ?>"> <?php echo $row['hotelname']; ?></a> </td>
<td>
<a href="ankit.php?id=<?php echo $row['id']; ?>"> <?php echo $row['hotelprofile']; ?></a>
</td>
<td>
<a href="ankit.php?id=<?php echo $row['id']; ?>"> <?php echo $row['address']; ?></a>
</td>
</tr>
<?php
}
echo '</table>';
?>
<?php
//$id=$_GET['id'];
if(isset($_GET['id']))
{
$id=$_GET['id'];
echo $id;
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "saurabh";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$sql="select * from hotel where id='$id'";
$result=mysqli_query($conn, $sql) or die(mysqli_error());
echo '<table><tr> <th> hotel name</th> <th>profiel </th><th>address </th> </tr>';
while($row=mysqli_fetch_array($result))
{
echo $row['hotelname'];
echo $row['hotelprofile'];
echo $row['address'];
}
}
?>
Demikianlah Artikel PHP Global Variables - Superglobals
Sekianlah artikel PHP Global Variables - Superglobals kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel PHP Global Variables - Superglobals dengan alamat link https://othereffect.blogspot.com/2016/05/php-global-variables-superglobals.html
0 Response to "PHP Global Variables - Superglobals"
Post a Comment