0% found this document useful (0 votes)
15 views132 pages

SL Unit-Iv (PHP)

Uploaded by

Priyanka Reddy
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)
15 views132 pages

SL Unit-Iv (PHP)

Uploaded by

Priyanka Reddy
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/ 132

PHP

html
Internet information services
Basic PHP Syntax

• A PHP script starts with <?php and ends with ?>:


<?php
// PHP code goes here
?>

• The default file extension for PHP files is ".php".

• PHP statements end with a semicolon (;).

• In PHP, keywords (e.g. if, else, while, echo, etc.),


classes, functions, and user-defined functions are
not case-sensitive.
• All variable names are case-sensitive
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.
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
Variables
• In PHP, a variable is declared using a $ sign followed
by the variable name.
• As PHP is a loosely typed language, so we do not
need to declare the data types of the variables. It
automatically analyzes the values and makes
conversions to its correct datatype.
• After declaring a variable, it can be reused
throughout the code.
• Assignment Operator (=) is used to assign the value
to a variable.

Syntax of declaring a variable in PHP is given below:


$variable_name=value;
Rules for declaring PHP variable

• A variable must start with a dollar ($) sign, followed by the variable
name.
• It can only contain alpha-numeric character and underscore (A-z, 0-
9, _).
• A variable name must start with a letter or underscore (_) character.
• A PHP variable name cannot contain spaces.
• the variable name cannot start with a number or special symbols.
• PHP variables are case-sensitive, so $name and $NAME both are
treated as different variable.
• <?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>
echo $y;
?>
Output:
Hello world!
5
10.5
PHP Variable: Sum of two variables

Filename: sum.php
• <?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>

Save: C:\xampp\htdocs\sum.php
Run : http://localhost/sum.php
PHP echo and print Statements
• In PHP, there are two basic ways to get output:
echo and print.
• PHP echo is a language construct, not a function. Therefore, you
don't need to use parenthesis with it.

• If you want to use more than one parameter, it is required to use


parenthesis.

The syntax of PHP echo:


void echo ( string $arg1 [, string $... ] )
• PHP echo statement can be used to print the string, multi-line
strings, escaping characters, variable, array, etc.

• echo is a statement, which is used to display the output.

• echo can be used with or without parentheses: echo(), and echo.

• echo does not return any value.

• We can pass multiple strings separated by a comma (,) in echo.

• echo is faster than the print statement.


Code: <?php
echo "Hello by PHP echo";
?>
Code:
<?php
$msg="Hello PHP";
echo "Message is: $msg";
?>
Program1 :
<?php
$txt1 = "Learn PHP";
$txt2 = “In an easy way";
$x = 5;
$y = 4;
echo "<h2>" . $txt1 . "</h2>";
echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
Program2 :

• <?php
print “ <h2> PHP is Fun! </h2>";
print "Hello world! <br>";
print "I'm about to learn PHP!";
?>
PHP Print

• PHP print is a language construct, so you don't need to use


parenthesis with the argument list.

• Print statement can be used with or without parentheses: print and


print().

• Print always returns 1.

• The syntax of PHP print :

int print(string $arg)


code:
<?php
print "Hello by PHP print ";
print ("Hello by PHP print()");
?>
code :
<?php
$msg="Hello print() in PHP";
print "Message is: $msg";
?>
• <?php
$txt1 = "Learn PHP";
$txt2 = “In an easy way";
$x = 5;
$y = 4;
print "<h2>" . $txt1 . "</h2>";
print "Study PHP at " . $txt2 . "<br>";
print $x + $y;
?>

-echo statement is faster than print statement


-print statement cannot take multiple values like echo
Difference between echo and print

echo print
• echo is a statement, which is • print is also a statement, used as
used to display the output. an alternative to echo at many
• echo can be used with or times to display the output.
without parentheses. • print can be used with or
• echo does not return any without parentheses.
value. • print always returns an integer
• value, which is 1.
We can pass multiple strings
separated by comma (,) in • Using print, we cannot pass
echo. multiple arguments.
• • print is slower than echo
echo is faster than print
statement.
statement.
• We can pass multiple arguments separated by a comma(,)in echo. It
will not generate any syntax error.

Program:
<?php
$fname=“raj”;
$lname=“kumar”;
echo ”my name is:”.$fname, $lname;
?>
PHP Variable Scope

The scope of a variable is defined as its range in the program


under which it can be accessed.
(Or)
The scope of a variable is the portion of the program within
which it is defined ad can be accessed.
PHP has three types of variable scopes:
1. Local variable
2. Global variable
3. Static variable
Local variable

• The variables that are declared within a function are


called local variables for that function.

• These local variables have their scope only in that


particular function in which they are declared. This
means that these variables cannot be accessed
outside the function, as they have local scope.
<?php
function myTest()
{
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
?>
Output:
Variable x inside function is:5
Global variable

• The global variables are the variables that are declared outside the
function.

• These variables can be accessed anywhere in the program.

• To access the global variable within a function, use the global


keyword before the variable.
<?php
$x=5;
$y=6;
function mytest()
{
global $x,$y;
$y=$x+$y;
}
mytest();
echo $y;
?>
Static variable

• feature of PHP to delete the variable, once it completes its execution


and memory is freed. Sometimes we need to store a variable even
after completion of function execution. Therefore, another important
feature of variable scoping is static variable. We use the static
keyword before the variable to define a variable, and this variable is
called as static variable.

• Static variables exist only in a local function, but it does not free its
memory after the program execution leaves the scope
<?php
function myTest()
{
static $x=0;
echo $x;
echo”<br>”;
$x++;
}
myTest();
myTest();
myTest();
?>
Output:
0
1
2
PHP Data Types
PHP data types are used to hold different types of data or
values. PHP supports 8 primitive data types that can be categorized
further in 3 types:

• Scalar Types (predefined)


• Compound Types (user-defined)
• Special Types
PHP Data Types: Scalar Types
It holds only single value. There are 4 scalar data types in PHP.
1. boolean
2.integer
3.float
4.string
PHP Data Types: Compound Types
It can hold multiple values. There are 2 compound data types in
PHP.
1.array
2.object
• PHP Data Types: Special Types
There are 2 special data types in PHP.
1.Resource
2.NULL
PHP Boolean
• Booleans are the simplest data type works like switch. It holds only
two values: TRUE (1) or FALSE (0).
• It is often used with conditional statements. If the condition is
correct, it returns TRUE otherwise FALSE.
<?php
if (TRUE)
echo "This condition is TRUE.";
if (FALSE)
echo "This condition is FALSE.";
?>
Output : This condition is TRUE.
PHP Integer

• Integer means numeric data with a negative or positive sign.


It holds only whole numbers, i.e., numbers without fractional
part or decimal points.
Rules for integer:
• An integer can be either positive or negative.
• An integer must not contain decimal point.
• Integer can be decimal (base 10), octal (base 8), or
hexadecimal (base 16).
PHP Float
• A floating-point number is a number with a decimal point. Unlike
integer, it can hold numbers with a fractional or decimal point,
including a negative or positive sign.
PHP String

• A string is a non-numeric data type. It holds letters or any alphabets,


numbers, and even special characters.
• String values must be enclosed either within single quotes or in
double quotes.
PHP Float

<?php
$n1 = 19.34;
$n2 = 54.472;
$sum = $n1 + $n2;
echo "Addition of floating numbers: " .$sum;
?>
Output:
Addition of floating numbers: 73.812
<?php
$str= “hello";
//both single and double quote statements will treat different
echo "Hai $str";
echo "</br>";
echo 'Hello $str';
?>
Output: Haihello
Hello$str
PHP Array
• An array stores multiple values in one single variable.
• The var_dump() function returns the data type and value.
<?php
$names = array(“ram",“raj",“ravi");
var_dump($names);
?>
Output:
array(3) {
[0]=> string(3) “ram"
[1]=> string(3) “raj"
[2]=> string(4) “ravi" }
PHP NULL Value

• Null is a special data type which can have only one value:
NULL.
• A variable of data type NULL is a variable that has no value
assigned to it.
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
Output: NULL
PHP Object

• Classes and objects are the two main aspects of object-oriented programming.
• A class is a template for objects, and an object is an instance of a class.
Resource
• Resource is a special data type that refers to any external resource.
• A resource variable acts as a reference to external source of data
such as stream, file, database etc.
• PHP uses relevant functions to create these resources.
• For example, fopen() function opens a disk file and its reference is
stored in a resource variable.
PHP Operators

Operators are used to perform operations on


variables and values.PHP divides the operators in the following groups:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators
Example

Output

Not Equal(because string type and integer type are not equal
=== checks datatype and value equality)
The precedence of “AND” operator is lower than the “&&”
Date function
• d - The day of the month (from 01 to 31)
• D - A textual representation of a day (three letters)
• j - The day of the month without leading zeros (1 to 31)
• l (lowercase 'L') - A full textual representation of a day
• N - The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
• S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works
well with j)
• w - A numeric representation of the day (0 for Sunday, 6 for Saturday)
• z - The day of the year (from 0 through 365)
• W - The ISO-8601 week number of year (weeks starting on Monday)
• F - A full textual representation of a month (January through December)
• m - A numeric representation of a month (from 01 to 12)
• M - A short textual representation of a month (three letters)
• n - A numeric representation of a month, without leading zeros (1 to 12)
• t - The number of days in the given month
• L - Whether it's a leap year (1 if it is a leap year, 0 otherwise)
Syntax
date(format, timestamp)
timestamp Optional. It Specifies an integer Unix
timestamp. Default is the current local time
(time())
Control Statements
if (condition)
{
code to be executed if condition is true;
} else
{
code to be executed if condition is false;
}
Example :
<?php
$t = date("H");
if ($t < "20")
{
echo "Have a good day!";
} else
{
echo "Have a good night!";
}
?>
PHP switch Statement

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;
}
<?php
$favcolor = "red";
switch ($favcolor)
{
case "red":
echo "Your favourite color is red!";
break;
case "blue":
echo "Your favourite color is blue!";
break;
case "green":
echo "Your favourite color is green!";
break;
default:
echo "Your favourite color is neither red, blue, nor green!";
}
?>output:
Your favourite color is red!
PHP Loops
Syntax:
while (condition is true)
{
code to be executed;
}
code
<? php
$x = 1;
while($x <= 5)
{ echo "The number is: $x";
$x++;
}
?>
Syntax:
do {
code to be executed;
} while (condition is true);
Example:
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Syntax:
for (init counter; test counter; increment counter) {
code to be executed;
}
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.
Example:
<?php
for ($x = 0; $x <= 10; $x++)
{
echo "The number is: $x <br>";
}
?>
output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
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.
Example:
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value)
{
echo "$value <br>";
}
?>
output:
red
green
blue
yellow
PHP User Defined Functions
• A function is a block of statements that can be used repeatedly
in a program.
• A function will not execute immediately when a page loads.
• A function will be executed by a call to the function.
Syntax:
function function_Name()
{
code to be executed;
}
Example:
<?php
function writeMsg()
{
echo "Hello world!";
}
writeMsg(); // call the function
?>
PHP Parameterized Function

• PHP Parameterized functions are the functions with parameters. You


can pass any number of parameters inside a function. These passed
parameters act as variables inside function.
• They are specified inside the parentheses, after the function name.
• The output depends upon the dynamic values passed as the
parameters into the function.
Parameterized functions
<?php
function sayHello($name)
{
echo ”hello $name<br>”;
}
SayHello(“Ravi”);
SayHello(“Raju”);
SayHello(“Rajan”);
?>
• <?php
function sayHello($name,$age)
{
echo ”hello $name , you are $age year old<br>”;
}
SayHello(“Ravi”,27);
SayHello(“Raju”,29);
SayHello(“Rajan”,23);
?>
output:
The height is : 350
The height is : 50
The height is : 135
The height is : 80
PHP Call By Value

• PHP allows you to call function by value and reference both. In case
of PHP call by value, actual value is not modified if it is modified
inside the function.
Program:
<?php
function fun($str2)
In this example, variable $str is passed to
{
the fun function where it is concatenated
$str2 .= 'Call By Value';
with 'Call By Value' string. But, printing
}
$str variable results 'Hello' only. It is
$str = 'Hello '; because changes are done in the local
fun($str); variable $str2 only. It doesn't reflect to
echo $str; $str variable.
?> output:Hello
<?php
function increment($i)
{
$i++;
}
$i = 10;
increment($i);
echo $i;
?>
Output: 10
Passing Arguments by Reference

• It is possible to pass arguments to functions by reference. This


means that a reference to the variable is manipulated by the function
rather than a copy of the variable's value.

• Any changes made to an argument in these cases will change the


value of the original variable. You can pass an argument by
reference by adding an ampersand to the variable name in the
function definition.
Dynamic Function Calls
Assign function names as strings to variables and then treat these variables
exactly as you would the function name itself.
PHP Arrays

• PHP array is used to hold multiple values of similar type in a single


variable.
Create an Array in PHP
In PHP, the array() function is used to create an array:
array();
PHP Array Types:
There are 3 types of array in PHP.
• Indexed arrays - Arrays with a numeric index
• Associative arrays - Arrays with named keys
• Multidimensional arrays - Arrays containing one or more arrays
PHP Indexed Arrays

There are two ways to create indexed arrays:


• The index can be assigned automatically (index always starts at 0),
like this:
$colors = array(“green", “red", “blue");
(Or)
the index can be assigned manually:
$colors[0] = “green";
$colors[1] = “red";
$colors[2] = “blue";
• The following example creates an indexed array named $colors,
assigns three elements to it, and then prints a text containing the
array values:
The Length of an Array - The count() Function

The count() function is used to return the length (the number of elements)
of an array
Loop Through an Indexed Array

To print all the values of an indexed array, use a for loop


PHP Associative Arrays

• Associative arrays are arrays that use named keys.


There are two ways to create an associative array:
$age = array(“ram"=>"35", “raj"=>"37", “ravi"=>"43");

(or)
• $age[‘ram'] = "35";
$age[‘raj'] = "37";
$age[‘ravi'] = "43";
First method to associate create array
second method to associate create array
PHP Multidimensional Array

• PHP multidimensional array is also known as array of arrays.


• It allows you to store tabular data in an array.
• PHP multidimensional array can be represented in the form of
matrix which is represented by row * column.
Definition
$emp = array (
array(1,“raj",400000),
array(2,“ravi",500000),
array(3,“ram",300000)
);
PHP String Functions

A string is a sequence of characters.


• strlen() - Return the Length of a String
• str_word_count() - Count Words in a String.
• strrev() function reverses a string.
• strpos() - Search For a Text Within a String
• str_replace() - Replace Text Within a String
• chop() - removes whitespaces or other predefined characters.

• chr()- Return characters from different ASCII values .


• join()-It is used to return a string from the elements of an array.
• implode() :Join array elements with a string:
strlen()

The PHP strlen() function returns the length of a string.


syntax: int strlen(string);
<?php
echo strlen("Hello world!"); // outputs 12
?>
str_word_count()

The PHP str_word_count() function counts the number of words in a string.

Syntax: int str_word_count(string);


<?php
echo str_word_count("Hello world!"); // outputs 2
?>
strrev()

The PHP strrev() function reverses a string


syntax: string strrev(string);
<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>
strpos()
• The PHP strpos() function searches for a specific text within a string.
• If a match is found, the function returns the character
position of the first match.
• If no match is fond, it will return FALSE.
Syntax:
int strpos(string ,find ,start);

String: specify the string to search(required).


Find :specify the string to find(required).
Start :specify where to begin the search(optional)
<?php
echo strpos("Hello world!", "world"); // outputs 6
?>
str_replace()

• The PHP str_replace() function replaces some characters


with some other characters in a string.
Syntax:
substr_replace ($search, replace , String, count);

$search : This parameter is a mandatory parameter. The $search parameter


contains The value which is going to be searched for the replacement in string.

$replace : This parameter is a mandatory parameter which is replaced with the


search values.

$string: This parameter is also a mandatory parameter which is array or string


in which search and replace value is searched and replaced.
$count: It is the last and optional parameter. It is an integer variable
which count the number of Replacement performed on a string $string.
Return Values:
• This function returns an array or a string with the replaced values
which is based on the $string parameter.

<?php
echo str_replace("world", "Dolly", "Hello world!");
// outputs Hello Dolly!
?>
PHP chop() Function

• The chop() function removes whitespaces or other predefined


characters from the right end of a string.
chop(string,charlist) ;
Parameter Values
•String Required. Specifies the string to check
•Charlist Optional. Specifies which characters to remove from the string.
program:

<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
• <?php
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"World!");
?>
Output:
Hello World!
Hello
PHP chr() Function

• Return characters from different ASCII values.


syntax: chr(ascii) ;
parameter:
ascii: Required. An ASCII value

• <?php
echo chr(52) . "<br>"; // Decimal value
echo chr(052) . "<br>"; // Octal value
echo chr(0x52) . "<br>"; // Hex value
?>
Output:
4
*
R
PHP string join() Function

• PHP string join() is predefined function.


• It is used to return a string from the elements of an array.
• It is an alias of the implode() function.
Syntax:
join(separator,array);
Parameters:

Separator : Specify the array element (optional)


Array :The array to join to a string (required)
Program 1:
<?php
$arr = array('Hello','PHP','Join','Function');
echo join(" ",$arr)."<br>";
?>
Output:
Hello PHP Join Function
Program 2:

<?php
$arr = array('Hello','PHP','Join','Function');
echo join("-",$arr)."<br>";
?>
Output:
Hello-PHP-Join-Function
implode() Function
• Join array elements with a string:
Syntax:
implode(separator,array);
Parameters:
•Separator Optional ,Specifies what to put between the array
elements. Default is "" (an empty string)
•Array Required. The array to join to a string
Progarm:
<?php
$arr = array('Hello','World!','Beautiful','Day!’);
echo implode(" ",$arr);
?>
Output: Hello World! Beautiful Day!
PHP ord() Function
• The ord() function returns the ASCII value of the first character of a
string.
Syntax:
ord(string);
Program:
<?php
echo ord("h")."<br>";
echo ord("hello")."<br>";
?>
output:
104
104
PHP strtolower() Function

• Convert all characters to lowercase.


Syntax: strtolower(string);
Program:
<?php
echo strtolower("Hello WORLD.");
?>
Ouput:
hello world.
strtoupper()
• The strtoupper() function converts a string to uppercase.
Syntax:
strtoupper(string) ;

Program:
<?php
echo strtoupper("Hello WORLD!");
?>
Output:
HELLO WORLD
PHP File Handling
• PHP File System allows us to create file, read file line by line, read file character by
character, write file, append file, delete file and close file.
• PHP has many functions to work with normal files. Those functions are:
fopen() –
• PHP fopen() function is used to open a file.
• First parameter of fopen() contains name of the file which is to be opened and
second parameter tells about mode in which file needs to be opened, e.g.,

Syntax: resource fopen ( $file, $mode);

Parameters:

$file: It is a mandatory parameter which specifies the file.


$mode: It is a mandatory parameter which specifies the access type of the
file or stream.
Return Value:
It returns a file pointer resource on success, or FALSE on error.

program:

<?php
$file = fopen(“demo.txt”,’w’);
?>
Files modes

• “w” – Opens a file for write only. If file not exist then new file
is created and if file already exists then contents of file is erased.
• “r” – File is opened for read only.
• “a” – File is opened for write only. File pointer points to end of
file. Existing data in file is preserved.
• “w+” – Opens file for read and write. If file not exist then new
file is created and if file already exists then contents of file is
erased.
• “r+” – File is opened for read/write.
• “a+” – File is opened for write/read. File pointer points to end
of file. Existing data in file is preserved. If file is not there then
new file is created.
fread()

After file is opened using fopen() the contents of data are read using
fread(). It takes two arguments.
syntax: string fread ( $file, $length );

Parameter:
$file: It is a mandatory parameter which specifies the file.
$length: It is a mandatory parameter which specifies the maximum
number of bytes to be read.
fwrite()

New file can be created or text can be appended to an existing file using
fwrite() function.
• Arguments for fwrite() function are file pointer and text that is to written
to file.
• It can contain optional third argument where length of text to written is
specified,

Syntax: fwrite(file, string, length) ;

Parameters: The fwrite() function in PHP accepts three parameters.


1. file : It is a mandatory parameter which specifies the file.
2. string : It is a mandatory parameter which specifies the string to be
written.
3. length : It is an optional parameter which specifies the maximum
number of bytes to be written.
Return Value: It returns the number of bytes written on success, or
False on failure.
fclose()

File is closed using fclose() function. Its argument is file which needs
to be closed, e.g.,

syntax: bool fclose( $file );

Program:
<?php
$file = fopen("demo.txt", 'r');
//some code to be executed
fclose($file);
?>
PHP Delete File - unlink()

• The PHP unlink() function is used to delete file.


Syntax:
bool unlink ( string $filename [, resource $context ] );
Program:
<?php
unlink(“data.txt”);
echo "File deleted successfully";
?>
PHP Read Single Line - fgets()

The fgets() function is used to read a single line from a file.


PHP Read Single Character - fgetc()
.
• The fgetc() function is used to read a single character from a file.
Program:

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open
file!");
// Output one character until end-of-file
while(!feof($myfile))
{
echo fgetc($myfile);
}
fclose($myfile);
?>
PHP Append to File

• Append data into file by using a or a+ mode in fopen() function.


• The PHP fwrite() function is used to write and append data into file.
data.txt
welcome to php file write.

Program:

<?php
$fp = fopen('data.txt', 'a');//opens file in append mode
fwrite($fp, ' this is additional text ');
fwrite($fp, 'appending data');
fclose($fp);

echo "File appended successfully";


?>
Output:welcome to php file write this is additional text appending data
PHP Form Handling

• We can create and use forms in PHP. To get form data, we need to
use PHP superglobals $_GET and $_POST.
• The form request may be get or post. To retrieve data from get
request, we need to use $_GET, for post request $_POST.
PHP Get Form

Get request is the default form request. The data passed through get
request is visible on the URL browser so it is not secured. You can send
limited amount of data through get request.
File: form1.html

<form action="welcome.php" method="get">


Name: <input type="text" name="name"/>
<input type="submit" value="visit"/>
</form>
welcome.php
<?php
$name=$_GET["name"];//
receiving name field value in $name variable
echo "Welcome, $name";
?>
PHP Post Form

• Post request is widely used to submit form that have large amount of
data such as file upload, image upload, login form, registration form
etc.
• The data passed through post request is not visible on the URL
browser so it is secured. You can send large amount of data through
post request.
• Let's see a simple example to receive data from post request in PHP.
form1.html
<form action="login.php" method="post">
<table>
<tr><td>Name:</td><td> <input type="text" name="name"/></td></
tr>
<tr><td>Password:</
td><td> <input type="password" name="password"/></td></tr>
<tr><td colspan="2"><input type="submit" value="login"/> </td></
tr>
</table>
</form>
login.php
<?php
$name=$_POST["name"];//
receiving name field value in $name variable
$password=$_POST["password"];//
receiving password field value in $password variable

echo "Welcome: $name, your password is: $password";


?>

You might also like