PHP Global Variables

Posted on Updated on

PHP Global Variables – Super globals

A few predefined variables in PHP are “superglobals”, which implies that they are constantly available, paying little respect to scope – and you can get to them from any function, class or record without doing anything extraordinary.

The PHP superglobal variables are:

$GLOBALS

$_SERVER

$_REQUEST

$_POST

$_GET

$_FILES

$_ENV

$_COOKIE

$_SESSION

This part will clarify a portion of the superglobals, and the rest will be clarified in later sections.

PHP $GLOBALS

$GLOBALS is a PHP super worldwide variable which is utilized to get to worldwide variables from anyplace in the PHP script (likewise from inside of capacities or strategies). PHP stores every single worldwide variable in a cluster called $GLOBALS[index]. The file holds the name of the variable.

The case underneath demonstrates to utilize the super worldwide variable $GLOBALS:

Case

<?php

$x = 75;

$y = 25;

function expansion() {

$GLOBALS[‘z’] = $GLOBALS[‘x’] + $GLOBALS[‘y’];

}

expansion();

echo $z;

? >

Run case »

In the case above, since z is a variable present inside of the $GLOBALS cluster, it is likewise open from outside the function!

PHP $_SERVER

$_SERVER is a PHP super worldwide variable which holds data about headers, ways, and script areas.

The case underneath demonstrates to utilize a portion of the components in $_SERVER:

Case

<?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’];

? >

Run case »

The accompanying table records the most essential components that can go inside $_SERVER:

Component/Code Description

$_SERVER[‘PHP_SELF’] Returns the filename of the as of now executing script

$_SERVER[‘GATEWAY_INTERFACE’] Returns the adaptation of the Common Gateway Interface (CGI) the server is utilizing

$_SERVER[‘SERVER_ADDR’] Returns the IP location of the host server

$_SERVER[‘SERVER_NAME’] Returns the name of the host server, (for example, www.w3schools.com)

$_SERVER[‘SERVER_SOFTWARE’] Returns the server recognizable proof string, (for example, Apache/2.2.24)

$_SERVER[‘SERVER_PROTOCOL’] Returns the name and update of the data convention, (for example, HTTP/1.1)

$_SERVER[‘REQUEST_METHOD’] Returns the solicitation technique used to get to the page, (for example, POST)

$_SERVER[‘REQUEST_TIME’] Returns the timestamp of the begin of the solicitation, (for example, 1377687496)

$_SERVER[‘QUERY_STRING’] Returns the question string if the page is gotten to by means of an inquiry string

$_SERVER[‘HTTP_ACCEPT’] Returns the Accept header from the present solicitation

$_SERVER[‘HTTP_ACCEPT_CHARSET’] Returns the Accept_Charset header from the present solicitation, (for example, utf-8,ISO-8859-1)

$_SERVER[‘HTTP_HOST’] Returns the Host header from the present solicitation

$_SERVER[‘HTTP_REFERER’] Returns the complete URL of the present page (not dependable in light of the fact that not all client operators bolster it)

$_SERVER[‘HTTPS’] Is the script questioned through a safe HTTP convention

$_SERVER[‘REMOTE_ADDR’] Returns the IP address from where the client is review the present page

$_SERVER[‘REMOTE_HOST’] Returns the Host name from where the client is survey the present page

$_SERVER[‘REMOTE_PORT’] Returns the port being utilized on the client’s machine to speak with the web server

$_SERVER[‘SCRIPT_FILENAME’] Returns the supreme pathname of the at present executing script

$_SERVER[‘SERVER_ADMIN’] Returns the worth given to the SERVER_ADMIN order in the web server arrangement record (if your script keeps running on a virtual host, it will be the quality characterized for that virtual host, (for example, someone@w3schools.com)

$_SERVER[‘SERVER_PORT’] Returns the port on the server machine being utilized by the web server for correspondence, (for example, 80)

$_SERVER[‘SERVER_SIGNATURE’] Returns the server form and virtual host name which are added to server-created pages

$_SERVER[‘PATH_TRANSLATED’] Returns the record framework based way to the present script $_SERVER[‘SCRIPT_NAME’] Returns the way of the present script

$_SERVER[‘SCRIPT_URI’] Returns the URI of the present page

PHP $_REQUEST

PHP $_REQUEST is utilized to gather information subsequent to presenting a HTML structure. The sample beneath demonstrates a structure with an information field and a submit catch. At the point when a client presents the information by tapping on “Present”, the structure information is sent to the record determined in the activity quality of the <form > tag. In this illustration, we indicate this document itself for handling structure information. On the off chance that you wish to utilize another PHP record to process structure information, supplant that with the filename of your decision.

At that point, we can utilize the super worldwide variable $_REQUEST to gather the estimation of the info field:

Sample

<html >

<body >

<form method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’];? > ” >Name: <input type=”text” name=”fname” >

<input type=”submit” >

< / form >

<?php

on the off chance that ($_SERVER[“REQUEST_METHOD”] == “POST”) {

/gather estimation of information field

$name = $_REQUEST[‘fname’];

on the off chance that (empty($name)) {

echo “Name is void”;

} else {

echo $name;

}

}

? >

< / body >

< / html >

Run illustration »

PHP $_POST

PHP $_POST is generally used to gather structure information in the wake of presenting a HTML structure with method=”post”. $_POST is likewise broadly used to pass variables.

The case beneath demonstrates a structure with an information field and a submit catch. At the point when a client presents the information by tapping on “Present”, the structure information is sent to the record determined in the activity property of the <form > tag. In this case, we indicate the record itself for handling structure information. On the off chance that you wish to utilize another PHP document to process structure information, supplant that with the filename of your decision.

At that point, we can utilize the super worldwide variable $_POST to gather the estimation of the data field:

Illustration

<html >

<body >

<form method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’];? > ” > Name: <input type=”text” name=”fname” >

<input type=”submit” >

< / form >

<?php

in the event that ($_SERVER[“REQUEST_METHOD”] == “POST”) {

/gather estimation of information field

$name = $_POST[‘fname’];

in the event that (empty($name)) {

echo “Name is void”;

} else {

echo $name;

}

}

? >

< / body >

< / html >

Run illustration »

PHP $_GET

PHP $_GET can likewise be utilized to gather structure information in the wake of presenting a HTML structure with method=”get”. $_GET can likewise gather information sent in the URL.

Accept we have a HTML page that contains a hyperlink with parameters:

<html >

<body >

<a href=”test_get.php?subject=PHP&web=W3schools.com” > Test $GET < / a >

< / body >

< / html >

At the point when a client taps on the connection “Test $GET”, the parameters “subject” and “web” is sent to “test_get.php”, and you can then get to their qualities in “test_get.php” with $_GET.

The sample beneath demonstrates the code in “test_get.php”:

Sample

<html >

<body >

<?php

echo “Study ” . $_GET[‘subject’] . ” at ” . $_GET[‘web’];

? >

< / body >

< / html >

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s