Unit III Updated
Unit III Updated
Example
<html>
<body>
<form action="form-handler.php" method="POST">
Name: <input type="text" name="name"> <br/>
Email: <input type="text" name="email"> <br/>
<input type="submit">
</form>
</body>
</html>
In the <form> tag, we have two attributes, action and method
action: Using this attribute, we can specify the name of the file
which will collect and handle the form-data. In the example
above, we have provided name of a PHP file.
</body>
</html>
Welcome.php
<html>
<body>
</body>
</html>
PHP Form Handling
The PHP superglobals $_GET and $_POST are used to collect form-
data.
$_GET
An associative array of variables passed to the current script via the HTTP GET method.
$_POST
An associative array of variables passed to the current script via the HTTP POST
method.
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.
PHP form handling
PHP Form Handling
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.
PHP $_GET
Both GET and POST create an array (e.g. array( key1 => value1,
key2 => value2, key3 => value3, ...)).
This array holds key/value pairs, where keys are the names of the
form controls and values are the input data from the user.
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
<html>
<body>
Welcome <?php echo $_GET[“fname”];
?>
</body>
</html>
Exercise:
<html>
<body>
Welcome <?php echo $_POST[“fname”];
?>
</body>
</html>
FORM Handling with GET
If we specify the form method to be GET, then the form-data is sent to the server using
the HTTP GET method.
PHP Global Variables - Superglobals
•$GLOBALS
•$_SERVER
•$_REQUEST
•$_POST
•$_GET
•$_FILES
•$_ENV
•$_COOKIE
•$_SESSION
PHP Global Variables - Superglobals
PHP $GLOBALS
function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
PHP Global Variables - Superglobals
PHP $_SERVER
$_SERVER is a PHP super global variable which holds
information about headers, paths, and script locations.
<?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'];
?>
PHP Global Variables - Superglobals
PHP $_REQUEST
PHP $_REQUEST is a PHP super global variable which is used to
collect data after submitting an HTML form.
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 code 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 code, we point to this file itself for processing form data. Then, we can use the
super global variable $_REQUEST to collect the value of the input field.
<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 = htmlspecialchars($_REQUEST['fname']);
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
FORM Handling Example
we will create a simple HTML form with different types of inputs and then write PHP code
for processing the form-data when submitted.
HTML FORM
<html>
<body>
<form action="form-handler.php" method="POST">
Name: <input type="text" name="name"> <br/>
Email: <input type="text" name="email"> <br/>
About Me:<br/>
<textarea name="aboutme"></textarea> <br/>
Gender:
<input type="radio" name="gender" value="female"> Female
<input type="radio" name="gender" value="male"> Male
<br/>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
FORM Output
PHP Form Validation
The HTML form contains various input fields: required and optional text
fields, radio buttons, and a submit button:
PHP Form Validation
The validation rules for the form above are as follows:
Text Fields
The name, email, and website fields are text input elements, and the
comment field is a textarea.
Radio Buttons
The gender fields are radio buttons and the HTML code looks like
this:
Gender:
When the form is submitted, the form data is sent with method="post".
explode(separator,string,limit)
Parameter Description
separator Required. Specifies where to break the string
string Required. The string to split
limit Optional. Specifies the number of array elements to
return.
PHP 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;
}
<?php
function writeMsg()
{
echo "Hello world!";
}
writeMsg();
?>
Hello world!
PHP Functions
<?php
function familyName($fname)
{
echo "$fname Refsnes.<br>";
}
<?php
function familyName($fname, $year)
{
echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege","1975");
familyName("Stale","1978");
familyName("Kai Jim","1983");
?>
Hege Refsnes. Born in 1975
Stale Refsnes. Born in 1978
Kai Jim Refsnes. Born in 1983
String Functions
Strings
Strings in PHP are also a collection of alphanumeric characters, enclosed in single
quotes for simple string data and double quotes for complex string data.
<?php
$srt1 = "This is a String";
$str2 = 'This is also a String';
?>
<?php
// string inside double quotes, with a single quote
$srt1 = "This is 'a' String";
// string inside single quotes, with a double quote
$str2 = 'This is also "a" String';
?>
String Functions
Example Code
<?php
// escaping double quote using backslash
$srt1 = "I\"ll handle this.";
// escaping single quote using backslash
$str2 = 'I\'ll handle this.';
echo $str1;
echo "\n"; // new line
echo $str2;
?>
String Functions
ord()
The ord() function returns the ASCII value of the first character
of a string.
Syntax
ord(string)
Parameter
String - The string to get an ASCII value from.
Return
The ord() function returns an integer between 0 and 255 that
would be the ASCII value of the first character of a string.
String Functions
Example
<?php
echo ord("h")."<br>"; //104
echo ord("hello")."<br>"; //104
?>
<?php
echo ord("welcome"); //119
?>
String Functions
Chr () function
<?php
$val = 119;
echo chr($val);
?>
OUTPUT
w
String Functions
PHP ltrim() Function
Remove characters from the left side of a string:
ltrim(string,charlist)
<?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>
Hello World!
World!
String Functions
PHP rtrim() Function
<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>
Hello World!
Hello
String Functions
Definition and Usage
The rtrim() function removes whitespace or other predefined
characters from the right side of a string.
Related functions:
<?php
$str = "Hello World! ";
echo "Without rtrim: " . $str;
echo "<br>";
echo "With rtrim: " . rtrim($str);
?>
substr(string,start,length)
String Functions
Parameter Description
<?php
// Positive numbers:
echo substr("Hello world",0,10)."<br>"; Hello worl
echo substr("Hello world",1,8)."<br>"; ello wor
echo substr("Hello world",0,5)."<br>"; Hello
echo substr("Hello world",6,6)."<br>"; world
echo "<br>";
Hello worl
ello wor
// Negative numbers:
Hello
echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
?>
String Functions
PHP strcmp() Function
strcmp(string1,string2)
<?php
echo strcmp("Hello world!","Hello world!");
?>
<?php
echo strcmp("Hello","Hello");
echo "<br>";
echo strcmp("Hello","hELLo");
?>
0
-32
String Functions
<?php
echo strcmp("Hello world!","Hello world!")."<br>"; // the two
strings are equal
echo strcmp("Hello world!","Hello")."<br>"; // string1 is greater
than string2
echo strcmp("Hello world!","Hello world! Hello!")."<br>"; //
string1 is less than string2
?>
0
7
-7
PHP Math Functions
PHP abs() Function
abs(number);
Parameter Description
<?php
echo(abs(6.7) . "<br>");
echo(abs(-6.7) . "<br>");
echo(abs(-3) . "<br>");
echo(abs(3));
?>
OUTPUT
6.7
6.7
3
3
PHP Math Functions
ceil(number);
Parameter Description
floor(number);
Parameter Description
number Required. Specifies the number to round down
PHP Math Functions
PHP floor() Function
<?php
echo(floor(0.60) . "<br>");
echo(floor(0.40) . "<br>");
echo(floor(5) . "<br>");
echo(floor(5.1) . "<br>");
echo(floor(-5.1) . "<br>");
echo(floor(-5.9));
?> 0
0
5
5
-6
-6
PHP Math Functions
PHP round() Function
round(number,precision,mode);
PHP Math Functions
PHP round() Function
round(number,precision,mode);
Parameter Description
number Required. Specifies the value to round
precision Optional. Specifies the number of decimal digits to round to.
Default is 0
mode •Optional. Specifies a constant to specify the rounding
mode:PHP_ROUND_HALF_UP - Default. Rounds number up
to precision decimal, when it is half way there. Rounds 1.5 to 2
and -1.5 to -2
•PHP_ROUND_HALF_DOWN - Round number down
to precision decimal places, when it is half way there. Rounds
1.5 to 1 and -1.5 to -1
•PHP_ROUND_HALF_EVEN -
Round number to precision decimal places towards the next
even value
•PHP_ROUND_HALF_ODD - Round number to precision decimal
places towards the next odd value
PHP Math Functions
PHP round() Function
<?php
echo(round(0.60) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.49) . "<br>");
echo(round(-4.40) . "<br>");
echo(round(-4.60));
?>
1
1
0
-4
-5
PHP Math Functions
fmod(x,y);
Parameter Description
<?php
echo(fmod(20, 4) . "<br>");
echo(fmod(20, 3) . "<br>");
echo(fmod(15, 6) . "<br>");
echo(fmod(-10, 3) . "<br>");
echo(fmod(0, 0));
?> 0
2
3
-1
NAN
PHP Math Functions
PHP min() Function
The min() function returns the lowest value in an array, or the lowest
value of several specified values.
min(array_values);
or
min(value1,value2,...);
Parameter Description
array_values Required. Specifies an array containing the values
value1,value2,. Required. Specifies the values to compare (must be at least two
.. values)
PHP Math Functions
PHP min() Function
<?php
echo(min(2,4,6,8,10) . "<br>");
echo(min(22,14,68,18,15) .
"<br>");
echo(min(array(4,6,8,10)) . "<br>");
echo(min(array(44,16,81,12)));
?>
2
14
4
12
PHP Math Functions
PHP max() Function
max(array_values);
or
max(value1,value2,...);
Parameter Description
array_values Required. Specifies an array containing the values
value1,value2 Required. Specifies the values to compare (must be at least
,... two values)
PHP Math Functions
PHP max() Function
<?php
echo(max(2,4,6,8,10) . "<br>");
echo(max(22,14,68,18,15) . "<br>");
echo(max(array(4,6,8,10)) . "<br>");
echo(max(array(44,16,81,12)));
?>
10
68
10
81
PHP Math Functions
PHP pow() Function
pow(x,y);
<?php
echo(pow(2,4) . "<br>");
echo(pow(-2,4) . "<br>");
echo(pow(-2,-4) . "<br>");
echo(pow(-2,-3.2));
?>
16
16
0.0625
NAN
PHP Math Functions
PHP sqrt() Function
sqrt(number);
Parameter Description
<?php
echo(sqrt(0) . "<br>");
echo(sqrt(1) . "<br>");
echo(sqrt(9) . "<br>");
echo(sqrt(0.64) . "<br>");
echo(sqrt(-9));
?> 0
1
3
0.8
NAN
PHP Math Functions
PHP rand() Function
Tip: If you want a random integer between 10 and 100 (inclusive), use
rand (10,100).
<?php
echo(rand() . "<br>");
echo(rand() . "<br>");
echo(rand(10,100));
?>
512549293
1132363175
79
PHP Array Functions
• Array()
• Count()
• list()
• in_array()
• current()
• Sort()
• each()
• next()
• Previous()
• rsort()
PHP list() Function
The list() function is used to assign values to a list of variables in
one operation.
Parameter Description
<?php
$my_array = array("Dog","Cat","Horse");
Parameter Description
search Required. Specifies the what to search for
array Required. Specifies the array to search
type Optional. If this parameter is set to TRUE, the
in_array() function searches for the search-string
and specific type in the array.
PHP in_array() Function
<?php
$people = array("Peter", "Joe", "Glenn",
"Cleveland");
if (in_array("Glenn", $people))
{
echo "Match found"; Match found
}
else
{
echo "Match not found";
}
?>
PHP current() Function
The current() function returns the value of the current element in
an array.
current(array)
Paramet Description
er
array Required. Specifies the array to use
PHP current() Function
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
Peter
PHP next() Function
The next() function moves the internal pointer to, and outputs,
the next element in the array.
next(array)
Paramet Description
er
array Required. Specifies the array to use
PHP next() Function
<?php
$people = array("Peter", "Joe", "Glenn",
"Cleveland");
Peter
Joe
PHP prev() Function
The prev() function moves the internal pointer to, and outputs,
the previous element in the array.
prev(array)
Parameter Description
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
Peter
Joe
Peter
PHP end() Function
The end() function moves the internal pointer to, and outputs,
the last element in the array.
end(array)
Parameter Description
array Required. Specifies the array to use
PHP end() Function
<?php
$people = array("Peter", "Joe", "Glenn",
"Cleveland");
Peter
Cleveland
PHP each() Function
The each() function returns the current element key and value, and
moves the internal pointer forward.
each(array)
Parameter Description
array Required. Specifies the array to use
PHP each() Function
<?php
$people = array("Peter", "Joe", "Glenn",
"Cleveland");
print_r (each($people));
?>
Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 )
PHP rsort() Function
rsort(array, sorttype)
<?php
$cars=array("Volvo","BMW",
"Toyota");
rsort($cars);
?>
Volvo
Toyota
BMW
PHP User Defined Function
• In PHP, the concept of the function is the same as in another language like
'C'. There are more than 1,000 in-built functions into the standard PHP
distribution.
• Besides these, we can define functions as per our requirements. These are
Syntax:
function function-name()
{
statement 1 :
statement 2 :
statement 3 :
......
}
PHP User Defined Function
Elements of a function
• The function body enclosed within a pair of braces which may contain
variable names an actual function code.
• The opening curly brace ( { ) indicates the beginning of the function
code and the closing curly ( } ) brace indicates the termination of the
function.
PHP User Defined Function
<?php
function myfunction()
{
echo "Good Morning";
}
myfunction();
?>
<?php
function function1()
{
function function2()
{
echo "Good Morning <br>";
}
}
function1();
function2();
?>
PHP User Defined Function
Syntax