PHP PPT
PHP PPT
PHP PPT
Charles Liu
Overview
1. Basics of HTTP
2. PHP syntax
3. Server-side programming
4. Connecting to MySQL
Request to a Static Site
Server:
1. Homepage
lookup
2. Send as HTTP
Response
HTTP Request: GET www.xkcd.com
Server:
HTTP Request: GET www.facebook.com
1. Look up things that go on
user’s profile, such as
wall posts and friends "
caches, database
lookups
You (client) Web server
2. Generate client-side
HTTP Response: web content (HTML file) code containing these
things
Client-side code: HTML, CSS, JavaScript
3. Send as HTTP response
(dynamically generated by server)
sample http interactions
Charles Liu
What is PHP?
side code
! mustend up with HTML, CSS, JavaScript, other client-
side code!
Side-by-side
Charles Liu
Variables
! Store values for future reference, use variable name
to refer to the value stored in it
$x = 42; // store the value 42 in $x
echo $x; // prints 42
echo $x+1; // prints 43, value of $x is still 42
$x = ‘hello!’ // type of $x can change
! $x += $y equivalent to $x = $x + $y
! Also works with subtraction, multiplication, division,
modulus, and string concatenation
== versus ===
! Two “equality” operators
! ==tests for “equality” in value but not necessarily type
! === tests for “identity” in value AND type
! String functions
! Length: strlen()
! Position of substring: strpos()
Charles Liu
Conditional Statements
if (condition / boolean expression) {
statements
}
else if (another condition) {
statements
}
// there may be more than one else if block
else {
statements
}
$x = 5;
if ($x == 5) {
echo ‘The variable x has value 5!’;
}
Loops
$x = 2;
while ($x < 1000) {
echo $x . “\n”; // \n is newline character
$x = $x * $x;
}
do {
echo $x . “\n”;
$x = $x * $x;
} while ($x < 1000); // note the semicolon
Charles Liu
Defining your own functions
function function_name ($arg1, $arg2) {
function code function parameters
return $var // optional
}
… // some code
function1();
echo $local_var; // does nothing, since $local_var is
// out of scope
?>
Global variable scope
! Variables declared outside a function have global
scope
! Use global keyword to gain access within functions
<?php
function function1() {
echo $a; // does nothing, $a is out of scope
global $a; // gain access to $a within function
echo $a; // prints 4
}
… // some code
$a = 4; // $a is a global variable
function1();
?>
PHP
Syntax: Arrays
Charles Liu
Arrays as a list of elements
! Use arrays to keep track of a list of elements using
the same variable name, identifying each element by
its index, starting with 0
$colors = array(‘red’, ‘blue’, ‘green’, ‘black’, ‘yellow’);
Charles Liu
Superglobals
! A few special associative arrays that can be
accessed from anywhere in a PHP file
<html>
<head><title>This is welcome.php</title></head>
<body>
The name that was submitted was:
<?php echo $_POST['name']; ?><br />
The phone number that was submitted was:
<?php echo $_POST['phone']; ?><br />
</body>
</html>
Charles Liu
Cookies and sessions
information
! Shoppingcart
! “Remember me” on login sites
value pairing
! Store user information as a value with a known key
! Never assume a cookie has been set. Always check with
isset($_COOKIE[$cookie_name]) before trying to use
the cookie’s value
The setcookie() function
! To set a cookie in PHP:
setcookie(name, value, expire, path, domain);
! Name and value correspond to $_COOKIE[$name] =
$value
! Expiration – cookie will no longer be read after the
expiration
! Useful to use time in seconds relative to the present:
# time() + time in seconds until expiration
! Path and domain refer to where on the site the cookie is
valid
! Usually ‘/’ for path and the top-level domain (yoursitename.com)
! To delete a cookie, set a new cookie with same arguments
but expiration in the past
Setting cookies
COOKIES SESSIONS
Where is data stored? Locally on client Remotely on server
Expiration? Variable – determined Session is destroyed
when cookie is set when the browser is
closed
Size limit? Depends on browser Depends only on server
(practically no size
limit)
Accessing information? $_COOKIE $_SESSION
General use? Remember small things Remembering varying
about the user, such as amount of data about
login name. Remember the user in one
things after re-opening browsing “session”.
browser More sensitive info.
PHP
MySQL
Charles Liu
Databases and MySQL
conditions
! CREATE TABLE table_name (column_name
reviews
id by content rating about
1 3 yummy! 5 2
2 2 neat 4 1
categories
id name
1 Food
2 Tech
3 Travel
query operators
relational algebra operators
› select: filter rows by a predicate
› project: filter by columns
› product: combine two tables
in SQL, all parts of select statement
-- show content and ratings of reviews about Clover
select content, rating from subjects, reviews
where subjects.id = reviews.about and name = "Clover"
deconstructing a query
subjects reviews
id by name category id by content rating about
1 3 Lucid 2 1 3 yummy! 5 2
2 2 Clover 1 2 2 neat 4 1
3 3 Cosi 1
categs
subjects users
reviews
special operators
› order by: sort the results by some column
› sum, avg, count, max, min
› group by: group rows before applying functions
-- show subjects and their average ratings
select name, avg(rating) from subjects, reviews
where reviews.about = subjects.id group by subjects.id
Charles Liu
What we’ve talked about…
! ASP.NET
! Python
! 35-225, 11AM