06 PHPIntro PDF
06 PHPIntro PDF
CS380
URLs and web servers
2
http://server/path/file
Usually when you type a URL in your browser:
Your computer looks up the server's IP address
using DNS
Your browser connects to that IP address and
requests the given file
The web server software (e.g. Apache) grabs that
file from the server's local file system
The server sends back its contents to you
CS380
URLs and web servers (cont.)
3
Apache,
Websphere
SW(Java Servlets,
XML Files)
http://www.facebook.com/home.ph
p
Some URLs actually specify programs that the
CS380
Server-Side web programming
5
CS380
Server-Side web programming
6
(cont.)
Also called server side scripting:
Dynamically edit, change or add any content to a
Web page
Respond to user queries or data submitted from
HTML forms
Access any data or databases and return the
results to a browser
Customize a Web page to make it more useful for
individual users
Provide security since your server code cannot be
viewed from a browser
Server-Side web programming
7
(cont.)
Web server:
contains software that allows it to run server side
programs
sends back their output as responses to web
requests
Each language/framework has its pros and
cons
we use PHP
CS380
What is PHP?
8
Hello.php
Hello world!
CS380
Hello World!
11
<?php
print "Hello, world!";
?>
PHP
Hello world!
output
CS380
Viewing PHP output
12
Hello world!
13 PHP Basic Syntax
CS380
PHP syntax template
14
HTML content
<?php
PHP code
?>
HTML content
<?php
PHP code
?>
HTML content ... PHP
print "text";
PHP
print "Hello, World!\n";
print "Escape \"chars\" are the SAME as in Java!\n";
print "You can have
line breaks in a string.";
print 'A string can use "single-quotes". It\'s cool!';
PHP
Hello world! Escape "chars" are the SAME as in Java! You can have line
breaks in a string. A string can use "single-quotes". It's cool!
output
CS380
Variables
16
$name = expression;
PHP
$user_name = “mundruid78";
$age = 16;
$drinking_age = $age + 5;
$this_class_rocks = TRUE;
PHP
names are case sensitive
names always begin with $, on both
declaration and usage
always implicitly declared by assignment (type
is not written)
a loosely typed language (like JavaScript or
Python)
Variables
17
+ - * / % . ++ --
= += -= *= /= %= .=
many operators auto-convert types: 5 + "7" is
12
CS380
Comments
19
# single-line comment
// single-line comment
/*
multi-line comment
*/
PHP
like Java, but # is also allowed
a lot of PHP code uses # comments instead of //
CS380
String Type
20
$favorite_food = "Ethiopian";
print $favorite_food[2];
$favorite_food = $favorite_food . " cuisine";
print $favorite_food;
PHP
# index 0123456789012345
$name = "Stefanie Hatcher";
$length = strlen($name);
$cmp = strcmp($name, "Brian Le");
$index = strpos($name, "e");
$first = substr($name, 9, 5);
$name = strtoupper($name);
PHP
CS380
String Functions (cont.)
22
CS380
Interpreted Strings
23
$age = 16;
print "You are " . $age . " years old.\n";
print "You are $age years old.\n"; # You are 16 years old.
PHP
CS380
Interpreted Strings (cont.)
24
PHP
if necessary to avoid ambiguity, can enclose
variable in {}
CS380
Interpreted Strings (cont.)
25
$name = “Xenia";
$name = NULL;
if (isset($name)) {
print "This line isn't going to be reached.\n";
} PHP
a variable is NULL if
it has not been set to any value (undefined
variables)
it has been assigned the constant NULL
CS380
bool (Boolean) type
27
$feels_like_summer = FALSE;
$php_is_great = TRUE;
$student_count = 7;
$nonzero = (bool) $student_count; # TRUE
PHP
the following values are considered to be
FALSE (all others are TRUE):
0 and 0.0 (but NOT 0.00 or 0.000)
"", "0", and NULL (includes unset variables)
if (condition) {
statements;
} elseif (condition) {
statements;
} else {
statements;
}
PHP
CS380
while loop (same as Java)
29
while (condition) {
statements;
} PHP
do {
statements;
} while (condition);
PHP
CS380
Math operations
30
$a = 3;
$b = 4;
$c = sqrt(pow($a, 2) + pow($b, 2));
PHP
math functions
math constants
CS380
Int and Float Types
31
$a = 7 / 2; # float: 3.5
$b = (int) $a; # int: 3
$c = round($a); # float: 4.0
$d = "123"; # string: "123"
$e = (int) $d; # int: 123 PHP
CS380
PHP exercise 1
32
PHP includes all the standard arithmetic operators. For this PHP
exercise, you will use them along with variables to print equations to
the browser. In your script, create the following variables:
$x=10;
$y=7;
Write code to print out the following:
10 + 7 = 17
10 - 7 = 3
10 * 7 = 70
10 / 7 = 1.4285714285714
10 % 7 = 3
Use numbers only in the above variable assignments, not in the
echo statements.
CS380
PHP exercise 3
34
CS380
PHP exercise 5
37
CS380
PHP exercise 7
39