Tutorial: Basant Namdeo Iips, Davv
Tutorial: Basant Namdeo Iips, Davv
Basant Namdeo
IIPS, DAVV
Itgives basic knowledge of PHP.
Gives some Example and understanding of the
Server side programming.
PHP stands for ‘PHP Hypertext Preprocessor’
It is Open-source, Server-side scripting language
Used to generate dynamic web-pages
PHP code is written between reserved PHP tags.
There can be both HTML and php code in the same file.
This file is saved by file extension .php.
Itis a Interpreted language. PHP scripts are parsed by
PHP interpreter(or PHP module in Web server) at run-
time when the page is requested by client browser.
It
is Executed on the server-side and the resulted
HTML contents are send to the client.
That’s why the Source-code not visible by client
‘View Source’ in browsers does not display the PHP code
Itis integrated with a number of popular databases,
including MySQL, PostgreSQL,Oracle, Sybase,
Informix , Microsoft SQL Server and many more.
PHP performs system functions, i.e. from files on a
system(web server) it can create, open , read , write
and close them.
PHP can handle forms , i.e. gather form data from
HTML form elements, save data to a file or process
that data to anyway you want.
You can add, delete, modify elements within your
database thru PHP.
Access cookies variables and set cookies.
Using PHP, you can restrict user to access some pages
of your website.
Structurally it is similar to C/C++.
Supports procedural and object-oriented paradigm (to some
degree).
All PHP statements end with a semi-colon.
Each PHP script must be enclosed in the reserved PHP tag.
This script block can be used any no. of times as and when
required with other HTML tags.
<?php
…Here Comes PHP Code….
?>
Comment Symbols just like C, C++, and shell comment
symbols
<?php
$age = 35; // Numerical variable
$name = “Basant”; // String variable
<?php
$string1=“Hello”;
$string2=“PHP”;
$string3=$string1 . “ ” . $string2;
print $string3;
?>
Hello PHP
Ifthe string has a set of double quotation marks
that must remain visible, use the \ [backslash]
before the quotation marks to ignore and display
them.
<?php
$heading=“\”Computer Science\””;
print $heading;
?>
“Computer Science”
PHP Control Structures
PHP also has many control structure as other programming
language have. There are if-else, switch case. There various types
of loops are also available for example for loop, foreach, while, do-
while etc.
Switch case
Switch($var)
{
case “val1”:
statement;
break;
case “val2”:
statement;
break;
default:
statement;
}
if (condition)
<?php
{
if($user==“IIPS”)
Statements; {
} print “Hello IIPS.”;
}
elseif(condition) else
{ {
print “You are not IIPS.”;
Statement;
}
} ?>
else
{
}
while (condition)
{
Statements;
}
for (intialize;condition; increment/decrement)
{ statements; <?php
} $count=0;
while($count<3)
{
print “hello PHP. ”;
$count += 1;
// $count = $count + 1;
// or
// $count++;
?>
2009/4/1 $datedisplay=date(“yyyy/m/d”);
print $datedisplay;
# If the date is April 1st, 2009
# It would display as 2009/4/1
$datedisplay=date(“l, F m, Y”);
Wednesday, April 1, 2009 print $datedisplay;
# If the date is April 1st, 2009
# Wednesday, April 1, 2009
M Jan
F January
m 01
n 1
Day of Month d 01
Day of Month J 1
Day of Week l Monday
Day of Week D Mon
Functions MUST be defined before then can be
called
Function headers are of the format
function functionName($arg_1, $arg_2, …, $arg_n)
Include (“footer.php”);
•Save it as session.php
<?php
session_start();
if (!$_SESSION["count"])
$_SESSION["count"] = 0;
if ($_GET["count"] == "yes")
$_SESSION["count"] = $_SESSION["count"] + 1;
echo "<h1>".$_SESSION["count"]."</h1>";
?>
<a href="session.php?count=yes">Click here to count</a>
Destroy PHP - Sessions
Destroying a Session
why it is necessary to destroy a session when the session will get destroyed when
the user closes their browser. Well, imagine that you had a session registered called
"access_granted" and you were using that to determine if the user was logged into
your site based upon a username and password. Anytime you have a login feature,
to make the users feel better, you should have a logout feature as well. That's
where this cool function called session_destroy() comes in handy. session_destroy()
will completely demolish your session (no, the computer won't blow up or self
destruct) but it just deletes the session files and clears any trace of that session.
NOTE: If you are using the $_SESSION superglobal array, you must clear the array
values first, then run session_destroy.
Here's how we use session_destroy():
Destroy PHP - Sessions
<?php
// start the session
session_start();
header("Cache-control: private"); //IE 6 Fix
$_SESSION = array();
session_destroy();
echo "<strong>Step 5 - Destroy This Session </strong><br />";
if($_SESSION['name']){
echo "The session is still active";
} else {
echo "Ok, the session is no longer active! <br />";
echo "<a href=\"page1.php\"><< Go Back Step 1</a>";
}
?>
PHP Overview
Easy learning
Syntax Perl- and C-like syntax. Relatively easy
to learn.
Large function library
Embedded directly into HTML
Interpreted, no need to compile
Open Source server-side scripting language
designed specifically for the web.
Conceived in 1994, now used on +10 million
web sites.
Outputs not only HTML but can output XML,
images (JPG & PNG), PDF files and even
Flash movies all generated on the fly. Can
write these files to the file system.
Supports a wide-range of databases
(20+ODBC).
PHP also has support for talking to other
services using protocols such as LDAP, IMAP,
SNMP, NNTP, POP3, HTTP.
First PHP script
Save as sample.php:
<!– sample.php -->
<html>
<body>
<strong>Hello World!</strong><br />
<?php
echo “<h2>Hello, World</h2>”;
?>
<?php
$myvar = "Hello World";
echo $myvar;
?>
</body>
</html>
Function: List content of table.
showtable.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
Student List is :</br>
<?php
$dbhost = 'localhost';
$dbuser = 'admin';
$dbpass = '';
$dbname = 'test';
$tableName = "studmast";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$conn)
die('Could not connect: ' . mysql_error());
if (!mysql_select_db($dbname))
die("Can't select database");
$result = mysql_query("SELECT * FROM {$tableName}");
if (!$result)
die("Query to show fields from table failed!" . mysql_error());
/*Creating Table Header*/
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$tableName}</h1>";
echo "<table border='1'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td><b>{$field->name}</b></td>";
}
echo "</tr>\n";
/*End of Table Header Section */
/*Creating Table Rows.......*/
while($row = mysql_fetch_array($result) )
{
echo "<tr>";
/*
echo "<td>{$row['stuid']}</td>";
echo "<td>{$row['stuname']}</td>";
echo "<td>{$row['cgpa']}</td>";
*/
echo "<td>".$row['stuid']."</td>";
echo "<td>".$row['stuname']."</td>";
echo "<td>".$row['cgpa']."</td>";
echo "</tr>\n";
}
mysql_free_result($result);
mysql_close($conn);
?>
</body>
</html>
mysql_connect()
mysql_select_db()
include()
mysql_query()
mysql_num_rows()
mysql_fetch_array()
mysql_close()
PHP began in 1995 when Rasmus Lerdorf developed
a Perl/CGI script toolset he called the Personal
Home Page or PHP
PHP 2 released 1997 (PHP now stands for Hypertex
Processor). Lerdorf developed it further, using C
instead
PHP3 released in 1998 (50,000 users)
PHP4 released in 2000 (3.6 million domains).
Considered debut of functional language and
including Perl parsing, with other major features
PHP5.0.0 released July 13, 2004 (113 libraries>1,000
functions with extensive object-oriented
programming)
PHP5.0.5 released Sept. 6, 2005 for maintenance
and bug fixes