PHP Class Lecture
PHP Class Lecture
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
2
3(+1) Tier architecture
voice
DHTML
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
5
Coding
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
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
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
23
Examples
How to output variables using PHP
• Echo has a shortcut syntax, but it only works with the “short
open tag” configuration enabled on the server. <?= $hits ?>
24