0% found this document useful (0 votes)
76 views

PHP Class Lecture

PHP was created in 1994 by Rasmus Lerdorf initially for server-side form generation and HTTP usage logging. PHP 2 added database support and other features in 1995. PHP 3 added support for multiple platforms and databases in 1998. PHP 4 became more integrated with web servers for efficiency in 2000. PHP 5 added object oriented programming and other features in 2004.

Uploaded by

Apsara .k.s
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

PHP Class Lecture

PHP was created in 1994 by Rasmus Lerdorf initially for server-side form generation and HTTP usage logging. PHP 2 added database support and other features in 1995. PHP 3 added support for multiple platforms and databases in 1998. PHP 4 became more integrated with web servers for efficiency in 2000. PHP 5 added object oriented programming and other features in 2004.

Uploaded by

Apsara .k.s
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Brief History of PHP

PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It


was initially developed for HTTP usage logging and server-side form
generation in Unix.

PHP 2 (1995) transformed the language into a Server-side embedded scripting


language. Added database support, file uploads, variables, arrays, recursive
functions, conditionals, iteration, regular expressions, etc.

PHP 3 (1998) added support for ODBC data sources, multiple platform support,
email protocols (SNMP,IMAP), and new parser written by Zeev Suraski and
Andi Gutmans .

PHP 4 (2000) became an independent component of the web server for added
efficiency. The parser was renamed the Zend Engine. Many security features
were added.

PHP 5 (2004) adds Zend Engine II with object oriented programming, robust
XML support using the libxml2 library, SOAP extension for interoperability
with Web Services, SQLite has been bundled with PHP
1
Why is PHP used?
Cross Platform

Runs on almost any Web server on several operating systems.


One of the strongest features is the wide range of supported databases

Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server

Operating Systems: UNIX (HP-UX,OpenBSD,Solaris,Linux), Mac OSX, Windows


NT/98/2000/XP/2003

Supported Databases: Adabas D, dBase,Empress, FilePro (read-only),


Hyperwave,IBM DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-
SQL, MySQL, ODBC, Oracle (OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid,
Sybase, Velocis,Unix dbm

2
3(+1) Tier architecture
voice
DHTML

touch Browser HTTP SQL


(IE, FireFox, PHP script Database
Opera)
Web Server Database
vision HTML (Apache, IIS) tables
Server
Desktop
(PC or MAC)

SMS
Web Service
SMS system
Client application

Remote services

3
http://www.onlamp.com/php/2001/02/22/graphics/php_f_1.gif
4
Performance
– Zdnet Statistics

• PHP pumped out about 47 pages/second


• Microsoft ASP pumped out about 43 pages/second
• Allaire ColdFusion pumped out about 29 pages/second
• Sun Java JSP pumped out about 13 pages/second

* From PHP HOWTO, July 2001

5
Coding

– Prototype your web pages first


• Separate the design of the site from the coding
– Turn repetitive code into functions
• Makes for more maintainable and reusable code
– Turn grunt code into functions
• Database access, configuration file access

6
Getting Started
1. How to escape from HTML and enter PHP mode
• PHP parses a file by looking for one of the special tags that
tells it to start interpreting the text as PHP code. The parser then executes all of
the code it finds until it runs into a PHP closing tag.
HTML PHP CODE HTML

<?php echo “Hello World”; ?>


Starting tag Ending tag Notes
<?php ?> Preferred method as it allows the use of
PHP with XHTML
<? ?> Not recommended. Easier to type, but has
to be enabled and may conflict with XML

<script language="php"> ?> Always available, best if used when


FrontPage is the HTML editor

<% %> Not recommended. ASP tags support was


added in 3.0.4

7
PHP details
• Procedural language
– Compare with Javascript which is event-driven
• C-like syntax - { } ;
• Extensive Function Library
• Good Web-server integration
– Script embedded in HTML
– Easy access to form data and output of HTML pages
• Not fully object-oriented
– Java is fully object oriented – all functions have to be in a
class
– In PHP, classes are additional but quite simple to use

8
What can it do?
• Dynamic generation of web-page content
• Database interaction
• Processing of user supplied data
• Email
• File handling
• Text processing
• Network interaction
• And more…

9
Fundamentals
• PHP is embedded within xhtml pages within
the tags: <?php … ?>
• The short version of these tags can also be
used: <? … ?>
• Each line of PHP is terminated, like MySQL,
with a semi-colon.

10
Literals..
• All strings must be enclosed in single of
double quotes: ‘Hello’ or “Hello”.
• Numbers are not in enclosed in quotes: 1 or
45 or 34.564
• Booleans (true/flase) can be written directly
as true or false.

11
Comments
// This is a comment
# This is also a comment
/* This is a comment
that is spread over
multiple lines */
• Do not nest multi-line comments
• // recommended over #

12
Displaying Data
• There are two language constructs available to
display data: print() and echo().
• They can be used with or without brackets.
• Note that the data ‘displayed’ by PHP is
actually parsed by your browser as HTML.
View source to see actual output.

13
Variables: What are they?
• When we work in PHP, we often need a
labelled place to store a value (be it a string,
number, whatever) so we can use it in
multiple places in our script.
• These labelled ‘places’ are called
VARIABLES

14
Variables: Naming
• $ followed by variable name
• Case sensitive
– $variable differs from $Variable
– Stick to lower-case to be sure!
• Name must started with a letter or an
underscore
– Followed by any number of letters, numbers and
underscores

15
Variables: example
<?php
$name = ‘Phil’;
$age = 23;
echo $name;
echo ’ is ‘;
echo $age;
// Phil is 23
?>
16
Constants
• Constants (unchangeable variables) can
also be defined.
• Each constant is given a name (note no
preceding dollar is applied here).
• By convention, constant names are
usually in UPPERCASE.

17
Constants
<?php
define(‘NAME’,‘Phil’);
define(‘AGE’,23);
echo NAME;
echo ’ is ‘;
echo AGE;
// Phil is 23
?>
18
“ or ‘ ?
• There is a difference between strings written
in single and double quotes.
• In a double-quoted string any variable names
are expanded to their values.
• In a single-quoted string, no variable
expansion takes place.

19
Getting Started
Simple HTML Page with PHP
The following is a basic example to output text using PHP.

<html><head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World!";
?>
</body></html>

Copy the code onto your web server and save it as “test.php”.
You should see “Hello World!” displayed.

Notice that the semicolon is used at the end of each line of PHP code to signify
a line break. Like HTML, PHP ignores whitespace between lines of code. (An
HTML equivalent is <BR>)
20
Getting Started
Using conditional statements

Conditional statements are very useful for


displaying specific content to the user. The
following example shows how to display content
according to the day of the week.
<?php
$today_dayofweek = date(“w”);
if ($today_dayofweek == 4){
echo “Today is Thursday!”;
}
else{
echo “Today is not Thursday.”;
}
?>

21
Getting Started
Using conditional statements
The if statement checks the value of $today_dayofweek
(which is the numerical day of the week, 0=Sunday… 6=Saturday)
• If it is equal to 4 (the numeric representation of Thurs.) it will
display
everything within the first { } bracket after the “if()”.
• If it is not equal to 4, it will display everything in the second { }
bracket
after the “else”.
<?php
$today_dayofweek = date(“w”);
if ($today_dayofweek == 4){
echo “Today is Thursday!”;
}
else{
echo “Today is not Thursday.”;
}
?>

22
Getting Started
Using conditional statements

If we run the script on a Thursday, we should see:


“Today is Thursday”.

On days other than Thursday, we will see:


“Today is not Thursday.”
<?php
$today_dayofweek = date(“w”);
if ($today_dayofweek == 4){
echo “Today is Thursday!”;
}
else{
echo “Today is not Thursday.”;
}
?>

23
Examples
How to output variables using PHP

• Echo is the common method in outputting data. Since it


is a language construct, echo doesn’t require parenthesis
like print().

• Output Text Usage:


<?php echo “Hello World”; ?> // prints out Hello World

• Output the value of a PHP variable:


<?php echo “$hits”; ?> // prints out the number of hits

• Echo has a shortcut syntax, but it only works with the “short
open tag” configuration enabled on the server. <?= $hits ?>

24

You might also like