0% found this document useful (0 votes)
30 views17 pages

8 Superglobals

The document explains PHP global variables, including how to access them within functions using the global keyword or $GLOBALS syntax. It also covers the PHP superglobals $_SERVER, $_POST, and $_GET, detailing how to use them for handling form data and the differences between GET and POST methods. Additionally, it highlights when to use each method based on data visibility and limitations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views17 pages

8 Superglobals

The document explains PHP global variables, including how to access them within functions using the global keyword or $GLOBALS syntax. It also covers the PHP superglobals $_SERVER, $_POST, and $_GET, detailing how to use them for handling form data and the differences between GET and POST methods. Additionally, it highlights when to use each method based on data visibility and limitations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

PHP GLOBAL VARIABLES

GLOBAL VARIABLES

• Global variables are variables that can be accessed


from any scope.
• Variables of the outer most scope are automatically
global variables, and can be used by any scope, e.g.
inside a function.
• To use a global variable inside a function you have to
either define them as global with the global keyword,
or refer to them by using the $GLOBALS syntax.
GLOBAL VARIABLES

The Code will result in error or no output


$x = 75;
function myfunction() {
echo $x;
}
myfunction()
GLOBAL VARIABLES

The Code will display 75


$x = 75;
function myfunction() {
echo $GLOBALS['x'];
}
myfunction()
GLOBAL VARIABLES

• You can also refer to global variables inside functions


by defining them as global with the global keyword.
$x = 75;
function myfunction() {
global $x;
echo $x;
}
myfunction()
CREATE GLOBAL VARIABLES

$x = 100;
echo $GLOBALS["x"];
echo $x;
CREATE GLOBAL VARIABLES

• Create a global variable from inside a function, and


use it outside of the function
function myfunction() {
$GLOBALS["x"] = 100;
}
myfunction();
echo $GLOBALS["x"];
echo $x;
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:
echo $_SERVER['PHP_SELF'];
echo $_SERVER['SERVER_NAME'];
echo $_SERVER['HTTP_HOST'];
echo $_SERVER['HTTP_REFERER'];
echo $_SERVER['HTTP_USER_AGENT'];
echo $_SERVER['SCRIPT_NAME'];
PHP $_POST

• $_POST contains an array of variables received via


the HTTP POST method.
• There are two main ways to send variables via the
HTTP Post method:
• HTML forms
• JavaScript HTTP requests
$_POST IN HTML FORMS

• A HTML form submits information via the HTTP POST


method if the form's method attribute is set to "POST".
<html><body>
<form method="POST" action="demo_request.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
</body></html>
$_POST IN HTML FORMS

• When a user clicks the submit button, the form data


is sent to a PHP file specified in the action attribute
of the <form> tag.
• In the action file we can use the $_POST variable to
collect the value of the input field.

$name = $_POST['fname'];
echo $name;
Example using $_POST in HTML Forms
<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 = htmlspecialchars($_POST['fname']);
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}?>
PHP $_GET

• $_GET contains an array of variables received via


the HTTP GET method.
• There are two main ways to send variables via the
HTTP GET method:
• Query strings in the URL
• HTML Forms
$_GET IN HTML FORMS
• A HTML form submits information via the HTTP GET method if
the form's method attribute is set to "GET".
<html><body>
<form action="welcome_get.php" method="GET">
Name: <input type="text" name="name">
E-mail: <input type="text" name="email">
<input type="submit">
</form>
</body></html>
GET vs POST

• Both GET and POST are treated as $_GET and


$_POST. These 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.
WHEN TO USE GET?

• Information sent from a form with the GET method is


visible to everyone (all variable names and values
are displayed in the URL). GET also has limits on the
amount of information to send.
• Because the variables are displayed in the URL, it is
possible to bookmark the page. This can be useful in
some cases.
• GET may be used for sending non-sensitive data.
WHEN TO USE POST?

• Information sent from a form with the POST method is


invisible to others (all names/values are embedded
within the body of the HTTP request) and has no limits
on the amount of information to send.
• POST supports advanced functionality such as support
for multi-part binary input while uploading files to
server.
• Because the variables are not displayed in the URL, it
is not possible to bookmark the page.

You might also like