web programming lecture 5
web programming lecture 5
What is PHP?
PHP is an acronym for "PHP: Hypertext Preprocessor".
PHP is a server-side and open source scripting language.
PHP is a powerful tool for making dynamic and interactive Web pages.
PHP is a widely-used, free, and efficient alternative to competitors such as
Microsoft's ASP.
PHP scripts are executed on the server.
PHP is free to download and use.
PHP 7 is the latest stable release.
History of PHP
1|Page
PHP Basics
Prepared By Dr. Tushar Kanti Saha
2|Page
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Note: We can use XAMPP Lite, WAMP to setup Apache and MySQL server at a time.
3|Page
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP Syntax
A PHP script is executed on the server, and the plain HTML result is sent back to
the browser.
<?php
// PHP code goes here
?>
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a built-
in PHP function "echo" to output the text "Hello World!" on a web page:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
4|Page
PHP Basics
Prepared By Dr. Tushar Kanti Saha
In the example below, all three echo statements below are equal and legal:
Example
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
Look at the example below; only the first statement will display the value of
the $color variable! This is because $color, $COLOR, and $coLOR are treated as three
different variables:
Example
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
5|Page
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP Comments
Comments in PHP
A comment in PHP code is a line that is not executed as a part of the program. Its
only purpose is to be read by someone who is looking at the code.
Comments can be used to:
Let others understand your code
Remind yourself of what you did - Most programmers have experienced
coming back to their own work a year or two later and having to re-figure
out what they did. Comments can remind you of what you were thinking
when you wrote the code
Example
Syntax for single-line comments:
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
?>
</body>
</html>
Example
Syntax for multiple-line comments:
<!DOCTYPE html>
<html>
<body>
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
</body>
</html>
6|Page
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example
Using comments to leave out parts of the code:
<!DOCTYPE html>
<html>
<body>
<?php
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
7|Page
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP Variables
Variables are "containers" for storing information.
Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
After the execution of the statements above, the variable $txt will hold the
value Hello world!, the variable $x will hold the value 5, and the variable $y will hold
the value 10.5.
Note: When you assign a text value to a variable, put quotes around the value.
Note: Unlike other programming languages, PHP has no command for declaring a
variable. It is created the moment you first assign a value to it.
PHP Variables
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
Variable names are case-sensitive ($age and $AGE are two different variables)
8|Page
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
Example
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
The following example will produce the same output as the example above:
Example
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
Example
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
Note: You will learn more about the echo statement and how to output data to the
screen in the next chapter.
PHP automatically associates a data type to the variable, depending on its value.
Since the data types are not set in a strict sense, you can do things like adding a
string to an integer without causing an error.
9|Page
PHP Basics
Prepared By Dr. Tushar Kanti Saha
In PHP 7, type declarations were added. This gives an option to specify the data type
expected when declaring a function, and by enabling the strict requirement, it will
throw a "Fatal Error" on a type mismatch.
You will learn more about strict and non-strict requirements, and data type
declarations in the PHP Functions chapter.
The scope of a variable is the part of the script where the variable can be
referenced/used.
local
global
static
Example
Variable with global scope:
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:
10 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example
Variable with local scope:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
You can have local variables with the same name in different functions, because
local variables are only recognized by the function in which they are declared.
To do this, use the global keyword before the variables (inside the function):
Example
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>
PHP also stores all global variables in an array called $GLOBALS[index]. The index holds
the name of the variable. This array is also accessible from within functions and
can be used to update global variables directly.
11 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example
<?php
$x = 5;
$y = 10;
function myTest() {
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}
myTest();
echo $y; // outputs 15
?>
To do this, use the static keyword when you first declare the variable:
Example
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
Then, each time the function is called, that variable will still have the information it
contained from the last time the function was called.
12 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
In this tutorial we use echo or print in almost every example. So, this chapter
contains a little more info about those two output statements.
The differences are small: echo has no return value while print has a return value of
1 so it can be used in expressions. echo can take multiple parameters (although such
usage is rare) while print can take one argument. echo is marginally faster than print.
Display Text
The following example shows how to output text with the echo command (notice
that the text can contain HTML markup):
Example
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
Display Variables
The following example shows how to output text and variables with
the echo statement:
Example
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
13 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
Display Text
The following example shows how to output text with the print command (notice
that the text can contain HTML markup):
Example
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
Display Variables
The following example shows how to output text and variables with
the print statement:
Example
<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>
14 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP String
A string is a sequence of characters, like "Hello world!". A string can be any text
inside quotes. You can use single or double quotes:
Example
<?php
$x = "Hello world!";
$y = 'Hello world!';
echo $x;
echo "<br>";
echo $y;
?>
PHP Integer
An integer data type is a non-decimal number between -2,147,483,648 and
2,147,483,647.
Rules for integers:
An integer must have at least one digit
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in: decimal (base 10), hexadecimal (base 16), octal
(base 8), or binary (base 2) notation
In the following example $x is an integer. The PHP var_dump() function returns the
data type and value:
15 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example
<?php
$x = 5985;
var_dump($x);
?>
PHP Float
A float (floating point number) is a number with a decimal point or a number in
exponential form.
In the following example $x is a float. The PHP var_dump() function returns the data
type and value:
Example
<?php
$x = 10.365;
var_dump($x);
?>
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
Booleans are often used in conditional testing. You will learn more about
conditional testing in a later chapter of this tutorial.
PHP Array
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump() function returns
the data type and value:
Example
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
16 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP Object
An object is a data type which stores data and information on how to process
that data.
In PHP, an object must be explicitly declared.
First we must declare a class of object. For this, we use the class keyword. A
class is a structure that can contain properties and methods:
Example
<?php
class Car {
function Car() {
$this->model = "VW";
}
}
// create an object
$herbie = new Car();
// show object properties
echo $herbie->model;
?>
Example
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
17 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP Operators
Operators are used to perform operations on variables and values.
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
18 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
The basic assignment operator in PHP is "=". It means that the left operand gets
set to the value of the assignment expression on the right.
x=y x=y The left operand gets set to the value of the
expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
19 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
20 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
21 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
22 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Very often when you write code, you want to perform different actions for different
conditions. You can use conditional statements in your code to do this.
Syntax
if (condition) {
code to be executed if condition is true;
}
Example
Output "Have a good day!" if the current time (HOUR) is less than 20:
<?php
$t = date("H");
23 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
Example
Output "Have a good day!" if the current time is less than 20, and "Have a good
night!" otherwise:
<?php
$t = date("H");
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if first condition is false and this condition is true;
} else {
code to be executed if all conditions are false;
}
Example
Output "Have a good morning!" if the current time is less than 10, and "Have a good
day!" if the current time is less than 20. Otherwise it will output "Have a good night!":
24 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
<?php
$t = date("H");
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
First, we have a single expression n (most often a variable), that is evaluated once.
The value of the expression is then compared with the values for each case in the
structure. If there is a match, the block of code associated with that case is executed.
Use break to prevent the code from running into the next case automatically.
The default statement is used if no match is found.
25 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
26 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP Loops
Often when you write code, you want the same block of code to run over and over
again a certain number of times. So, instead of adding several almost equal code-
lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a
certain condition is true.
while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as
long as the specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
The following chapters will explain and give examples of each loop type.
Syntax
while (condition is true) {
code to be executed;
}
Example:
The example below displays the numbers from 1 to 5:
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
27 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example Explained
$x = 1; - Initialize the loop counter ($x), and set the start value to 1
$x <= 5 - Continue the loop as long as $x is less than or equal to 5
$x++; - Increase the loop counter value by 1 for each iteration
Example
<?php
$x = 0;
while($x <= 100) {
echo "The number is: $x <br>";
$x+=10;
}
?>
Example Explained
$x = 0; - Initialize the loop counter ($x), and set the start value to 0
$x <= 100 - Continue the loop as long as $x is less than or equal to 100
$x+=10; - Increase the loop counter value by 10 for each iteration
The do...while loop - Loops through a block of code once, and then repeats
the loop as long as the specified condition is true.
Syntax
do {
code to be executed;
} while (condition is true);
Examples
The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will
write some output, and then increment the variable $x with 1. Then the condition is
checked (is $x less than, or equal to 5?), and the loop will continue to run as long as
$x is less than, or equal to 5:
28 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Note: In a do...while loop the condition is tested AFTER executing the statements
within the loop. This means that the do...while loop will execute its statements at
least once, even if the condition is false. See example below.
This example sets the $x variable to 6, then it runs the loop, and then the condition
is checked:
Example
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
}
Parameters:
init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the
loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value
29 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Examples
The example below displays the numbers from 0 to 10:
Example
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
Example Explained
$x = 0; - Initialize the loop counter ($x), and set the start value to 0
$x <= 10; - Continue the loop as long as $x is less than or equal to 10
$x++ - Increase the loop counter value by 1 for each iteration
Example
<?php
for ($x = 0; $x <= 100; $x+=10) {
echo "The number is: $x <br>";
}
?>
Example Explained
$x = 0; - Initialize the loop counter ($x), and set the start value to 0
$x <= 100; - Continue the loop as long as $x is less than or equal to 100
$x+=10 - Increase the loop counter value by 10 for each iteration
30 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Syntax
foreach ($array as $value) {
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to
$value and the array pointer is moved by one, until it reaches the last array
element.
Examples
The following example will output the values of the given array ($colors):
Example
<?php
$colors = array("red", "green", "blue", "yellow");
The following example will output both the keys and the values of the given array
($age):
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
31 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>
PHP Continue
The continue statement breaks one iteration (in the loop), if a specified condition
occurs, and continues with the next iteration in the loop.
This example skips the value of 4:
Example
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
?>
32 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Break Example
<?php
$x = 0;
while($x < 10) {
if ($x == 4) {
break;
} echo "The number is: $x <br>";
$x++;
}
?>
Continue Example
<?php
$x = 0;
while($x < 10) {
if ($x == 4) {
$x++;
continue;
}
echo "The number is: $x <br>";
$x++;
}
?>
33 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP Functions
The real power of PHP comes from its functions.
PHP has more than 1000 built-in functions, and in addition you can create
your own custom functions.
Syntax
function functionName() {
code to be executed;
}
Note: A function name must start with a letter or an underscore. Function names
are NOT case-sensitive.
In the example below, we create a function named "writeMsg()". The opening curly
brace ( { ) indicates the beginning of the function code, and the closing curly brace
( } ) indicates the end of the function. The function outputs "Hello world!". To call
the function, just write its name followed by brackets ():
Example
<?php
function writeMsg() {
echo "Hello world!";}
writeMsg(); // call the function
?>
34 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP Arrays
An array stores multiple values in one single variable:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in
single variables could look like this:
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
However, what if you want to loop through the cars and find a specific one? And
what if you had not 3 cars, but 300?
An array can hold many values under a single name, and you can access the values
by referring to an index number.
array();
35 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
The following example creates an indexed array named $cars, assigns three
elements to it, and then prints a text containing the array values:
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
36 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
$arrlength = count($cars);
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
37 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP supports multidimensional arrays that are two, three, four, five, or more levels
deep. However, arrays more than three levels deep are hard to manage for most
people.
Volvo 22 18
BMW 15 13
Saab 5 2
Land Rover 17 15
We can store the data from the table above in a two-dimensional array, like this:
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
Now the two-dimensional $cars array contains four arrays, and it has two indices:
row and column.
To get access to the elements of the $cars array we must point to the two indices
(row and column):
38 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
Example
<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
We can also put a for loop inside another for loop to get the elements of the $cars
array (we still have to point to the two indices):
Example
<?php
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}
?>
To know about some array functions, visit:
https://www.w3schools.com/php/php_ref_array.asp
39 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
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;
?>
In the example above, since z is a variable present within the $GLOBALS array, it is
also accessible from outside the function!
40 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
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
<?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'];
?>
The following table lists the most important elements that can go inside $_SERVER:
Element/Code Description
41 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
42 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP $_REQUEST
PHP $_REQUEST is a PHP super global variable which 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>
43 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP $_POST
PHP $_POST is a PHP super global variable which is 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>
<?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>
44 | P a g e
PHP Basics
Prepared By Dr. Tushar Kanti Saha
PHP $_GET
PHP $_GET is a PHP super global variable which is used to collect form data after
submitting an HTML form with method="get".
<html>
<body>
<a href="test_get.php?subject=PHP&web=W3schools.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.
Example
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>
45 | P a g e