PHP: Introduction: by Girisan E K Associate Professor & Head Dept of Computer Applications
PHP: Introduction: by Girisan E K Associate Professor & Head Dept of Computer Applications
By
Girisan E K
Associate Professor & Head
Dept of Computer Applications
Topics Covered
Server side scripting language
Client/Server systems
Comparison with static HTML
PHP - What is it? What does it do?
PHP Language basics
Syntax
Variables
Constants
Operators
Decision making
PHP and the client
Client/Server on the WWW
Standard web sites operate on a
request/response basis
A user requests a resource E.g. HTML
document
Server responds by delivering the document
to the client
The client processes the document and
displays it to user
Server Side Programming
Provides web site developers to utilise resources on
the web server
Non-public resources do not require direct access
from the clients
Allows web sites to be client agnostic (unless
JavaScript is used also)
Most server side programming script is embedded
within markup (although does not have to be,
sometimes better not to)
PHP - What is it / does it do?
PHP: PHP Hypertext Pre-processor
Programming language that is interpreted
and executed on the server
Execution is done before delivering content to
the client
Contains a vast library of functionality that
programmers can harness
Executes entirely on the server, requiring no
specific features from the client
PHP - What is it / does it do?
Static resources such as regular HTML are simply output to
the client from the server
Dynamic resources such as PHP scripts are processed on the
server prior to being output to the client
PHP has the capability of connecting to many database
systems making the entire process transparent to the client
PHP Engine –
Run Script
HTML Response PHP Results
User Web Server
PHP Summary
PHP: PHP Hypertext Pre-processor
Interpreted and executed by the server on
page request
Returns simple output to the client
Provides a tremendous amount of
functionality to programmers
Can connect transparently to many database
systems
PHP Language Basics
Look at the building blocks of the PHP
language
Syntax and structure
Variables, constants and operators
Data types and conversions
Decision making IF and switch
Interacting with the client application (HTML
forms)
PHP - Syntax and Structure
PHP is similar to C
All scripts start with <?php and with with ?>
Line separator: ; (semi-colon)
Code block: { //code here } (brace brackets)
White space is generally ignored (not in strings)
Comments are created using:
// single line quote
/* Multiple line block quote */
Precedence
Enforced using parentheses
E.g. $sum = 5 + 3 * 6; // would equal 23
$sum = (5 + 3) * 6; // would equal 48
PHP - Variables
Prefixed with a $
Assign values with = operator
Example: $author = “Adams”;
No need to define type
Variable names are case sensitive
$author and $Author are different
PHP - Example Script
<?php
$author = “Trevor Adams”;
$msg = “Hello world!”;
echo $author . “ says ” . $msg;
?>
PHP - Constants
Constants are special variables that cannot
be changed
Use them for named items that will not
change
Created using a define function
define(‘milestokm’, 1.6);
Used without $
$km = 5 * milestokm;
PHP - Operators
Standard mathematical operators
+, -, *, / and % (modulus)
String concatenation with a period (.)
$car = “SEAT” . “ Altea”;
echo $car; would output “SEAT Altea”
Basic Boolean comparison with “==”
Using only = will overwrite a variable value
Less than < and greater than >
<= and >= as above but include equality
PHP - Data Types
PHP is not strictly typed
Different to JAVA where all variables are declared
A data type is either text or numeric
PHP decides what type a variable is
PHP can use variables in an appropriate way automatically
E.g.
$vat_rate = 0.175; /* VAT Rate is numeric */
echo $vat_rate * 100 . “%”; //outputs “17.5%”
$vat_rate is converted to a string for the purpose of the
echo statement
Object, Array and unknown also exist as types, Be
aware of them but we shall not explore them today
PHP - embedded language
PHP can be placed directly inside HTML E.g.
<html>
<head><title>Basic PHP page</title></head>
<body>
<h1><?php echo “Hello World!; ?></h1>
</body>
</html>
Expressions
• Expressions evaluate to a value. The value can be a
string, number, boolean, etc...
• Expressions often use operations and function calls,
and there is an order of evaluation when there is more
than one operator in an expression
• Expressions can also produce objects like arrays
Hig
h
Lo
w
Control Structures
Two-way
using else X= 4
:
no yes
x>2
$x = 4;
no
$x = 7; yes
1 times 6 is 6
2 times 6 is 12
3 times 6 is 18
A for loop is the simplest 4 times 6 is 24
way to construct a counted 5 times 6 is 30
loop. 6 times 6 is 36
Loop Controls
• Like many C-inspired languages, PHP has two control
structures that work within a loop
• break - exit the loop immediately
• continue - finish the current iteration and jump to the next
iteration, starting at the top of the loop
Breaking Out of a Loop
• The break statement ends the current loop and jumps to the
statement immediately following the loop
• It is like a loop test that can happen anywhere in the body of the
loop