
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Variables from External Sources
Introduction
PHP's variable namespace is populated by external sources such as HTML form elements, cookies and screen coordinates of image submit button
HTML form elements
When a web page submits data in its HTML form to a PHP script, it is automatically made available to the script in the form of $_POST, $_GET and $_REQUEST variables. Following is a typical HTML form
<form action="testscript.php" method="POST"> <input type="text" name="name"> <input type="text" name="age"> <input type ="submit" valaue="submit"> </form>
Data entered by user is populated as $_POST associative array in PHP script
<?php echo "Name : " . $_POST["name"] . "<br>"; echo "Age : " . $_POST["age"]; ?>
Place HTML page in document root along with testscript.php. Open it in browser and enter data
Name : xyz Age : 20
Using method='GET' in HTML form causes URL in action attribute to be requested using HTTP GET method. Data in the form is populated in $_GET array. The $_REQUEST array provides contents of $_GET, $_POST and $_COOKIE predefined variables. For example, data in form element named 'age' will be available as $_GET['age'] and $_REQUEST['age']
Imaage button coordinates
In standard submit button, HTML allows any image to be used as button with image input type
<input type="image" src="image.gif" name="sub" />
In this case, when user clicks on the image x and y coordinates of screen dimensions are also sent as request and can be accessed as $_POST['sub_x'] and $_POST['sub_y']
Cookie variables
PHP supports mechanism of storage and retrieval of cookies. Cookie is a data stored by the server in client's computer while sending response. Every subsequent request by the client sends back the cookies along with requested parameters such as HTML form elements. PHP uses Setcookie() function to store cookie. Cookies are read in $_COOKIE array. Following is a simple example
Example
<?php if (isset($_COOKIE['name']) && isset($_COOKIE['age'])) { echo "Name:" .$_COOKIE['name'] . " age:" .$_COOKIE['age']; } setcookie('name', 'XYZ'); setcookie('age', 20); ?>
When above script is called from browser for first time, cookies name and age are set. Subsequently, they are transmitted to the server in $_COOKIE array and they are displayed as under
Output
Name:XYZ age:20
- Related Articles
- Access variables from parent scope in anonymous PHP function
- PHP Predefined Variables
- PHP variable Variables
- Multiple index variables in PHP foreach loop
- How to pass JavaScript variables to PHP?
- How to ping external IP from java Android?
- Name the pollutants which are added from natural sources.
- How to Ping External host from Swift in iOS?
- Generating SAP ABAP code/ script from XML coming from an external application
- Which is faster? Constants, Variables or Variable Arrays in PHP?
- Coherent Sources
- Manipulate for loop to run code with specific variables only PHP?
- PHP – Retrieve internal configuration variables of iconv extension using iconv_get_encoding() function
- MySQL Information Sources
- Sources of Stress
