06_PHP_Lecture
06_PHP_Lecture
iFour Consultancy
HYPERTEXT PREPROCESSOR
web Engineering ||
winter 2017
wahidullah Mudaser Lecture 06
[email protected]
Cont PHP
INDEX
PHP Arrays
Types Of PHP Arrays
PHP Global Arrays
PHP Exception Handling
Assignment
Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
PHP Arrays
The count() function is used to return the length (the number of elements) of
an array:
Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
Loop Through an Indexed Array
To loop through and print all the values of an indexed array, you could use a
for loop, like this:
Example:
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
$age = array(“ahmad"=>"35", “khalid"=>"37", “nazir"=>"43");
or:
$age[‘ahmad'] = "35";
$age[‘khalid'] = "37";
$age[‘nazir'] = "43";
PHP Associative Arrays
Example1
<?php
$age = array(“ahmad"=>"35", “khalid"=>"37", “nazir"=>"43");
echo "Peter is " . $age[‘ahmad'] . " years old.";
?>
Example2
<?php
$age = array(“ahmad"=>"35", “khalid"=>"37", “nazir"=>"43");
$_SERVER is a PHP super global variable which holds information about headers,
paths, and script locations.
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'];
?>
BY
PRATIK TAMBEKAR
HEMANT HINGAVE
PHP $_REQUEST
PHP $_REQUEST is used to collect data after submitting an HTML form.
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>
PHP $_POST
PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to
pass variables.
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 = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
PHP $_GET
PHP $_GET can also be used to collect form data after submitting an
HTML form with method="get".
$_GET can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with
parameters:
<html>
<body>
It is possible to insert the content of one PHP file into another PHP file
(before the server executes it), with the include or require statement.
Syntax:
include 'filename';
or
require 'filename';
PHP Exception Handling
<?php
//create function with an exception
function checkNum($number) {
if($number>1) {
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception in a "try" block
try {
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below'; }
//catch exception
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
} ?>
TODAY TASK
Create One User Registration Form having fields
Name, Age, Address, Cell Number, College Name , Designation etc.
And Print the above data on the same form at the bottom of the form in Table
Format.
Questions?