PHP
iFour Consultancy
HYPERTEXT PREPROCESSOR
web Engineering ||
winter 2017
wahidullah Mudaser Lecture 06
assignment.cs2017@gmail.com
Cont PHP
INDEX
PHP Arrays
Types Of PHP Arrays
PHP Global Arrays
PHP Exception Handling
Assignment
http://www.ifourtechnolab.com/ ASP.NET Software Development Companies India
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] . ".";
?>
PHP Arrays
Create an Array in PHP
In PHP, the array() function is used to create an array:
array();
In PHP, there are three types of arrays:
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:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
Get The Length of an Array - The count() Function
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);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
PHP Associative Arrays
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");
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
PHP - Sort Functions For Arrays
The following PHP array sort functions:
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
PHP Global Variables - Superglobals
Several predefined variables in PHP are "superglobals", which means that they are
always accessible, regardless of scope - and you can access them from any function,
class or file without having to do anything special.
The PHP superglobal variables are:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
PHP $_SERVER
$_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>
<a href="test_get.php?subject=PHP&web=ycce.com">Test $GET</a>
</body>
</html>
PHP $_GET
When a user clicks on the link "Test $GET", the parameters "subject" and
"web" is sent to "test_get.php", and you can then access their values in
"test_get.php" with $_GET.
The example below shows the code in "test_get.php":
Example:
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body> </html>
PHP Date and Time
The PHP Date() Function
The PHP date() function formats a timestamp to a more readable date and
time.
Syntax:
date(format,timestamp)
Example:
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l"); // l represents Day of week
?>
PHP Tip - Automatic Copyright Year
Use the date() function to automatically update the copyright year on
your website:
Example:
© 2010-<?php echo date("Y")?>
Get a Simple Time
Example
<?php
echo "The time is " . date("h:i:sa");
?>
PHP 5 Include Files
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?
http://www.ifourtechnolab.com/ ASP.NET Software Development Companies India