PHP

CSE 190 M (Web Programming), Spring 2008

University of Washington

Except where otherwise noted, the contents of this presentation are © Copyright 2008 Marty Stepp and Jessica Miller and are licensed under the Creative Commons Attribution 2.5 License.

Valid XHTML 1.0 Strict Valid CSS!

What is PHP?

PHP logo

Why server-side programming?

JavaScript already allows us to create dynamic, programmable web pages. Why use a server-side language instead of JavaScript?

Why PHP?

There are many other options for server-side languages: Ruby on Rails, JSP, ASP.NET, etc. Why choose PHP?

PHP vs. JavaScript

Hello, World!

The following contents could go into a file hello.php:

<?php
header("Content-type: text/plain");

print "Hello, world!\n";
print "\n";
print "This is my first PHP program.\n";
?>

Web servers and PHP

PHP server

Viewing PHP output

PHP local output PHP server output

Basic PHP syntax

Creating simple PHP script programs

Console output: print

print "text";
print "Hello, World!";
print "Escape \"chars\" are the SAME as in Java!\n";

print "You can have
line breaks in the string
and they'll show up";

print 'A string can use "single-quotes".  It\'s cool!';

Headers

header("Content-type: text/plain");

Variables

$name = expression;
$user_name = "PinkHeartLuvr78";
$age = 16;
$drinking_age = $age + 5;
$this_class_rocks = TRUE;

Types

Operators

int and float types

$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

Math operations

$a = 3;
$b = 4;
$c = sqrt(pow($a, 2) + pow($b, 2));

Comments

# single-line comment

// single-line comment

/*
multi-line comment
*/

String type

$favorite_food = "Ethiopian";
print $favorite_food[2];            # h

String functions

$name = "Kenneth Kuan";
$length = strlen($name);              # 12
$cmp = strcmp($name, "Jeff Prouty");  # > 0
$index = strpos($name, "e");          # 1
$first = substr($name, 8, 4);         # "Kuan"
$name = strtoupper($name);            # "KENNETH KUAN"
NameJava/JS Equivalent
explode, implode split, join
strlen length
strcmp compareTo
strpos indexOf
substr substring
strtolower, strtoupper toLowerCase, toUpperCase
trim substring

Interpreted strings

$age = 16;
print "You are " . $age . " years old.\n";
print "You are $age years old.\n";    # You are 16 years old.

for loop (same as Java/JS)

for (initialization; condition; update) {
	statements;
}
for ($i = 0; $i < 10; $i++) {
	print "$i squared is " . $i * $i . ".\n";
}

bool (Boolean) type

$feels_like_summer = FALSE;
$php_is_rad = TRUE;

$student_count = 96;
$nonzero = (bool) $student_count;     # TRUE
  • TRUE and FALSE keywords are case insensitive

if/else statement

if (condition) {
	statements;
} elseif (condition) {
	statements;
} else {
	statements;
}

while loop (same as Java/JS)

while (condition) {
	statements;
}
do {
	statements;
} while (condition);

NULL

Functions

function name(parameterName, ..., parameterName) {
	statements;
}
function quadratic($a, $b, $c) {
	return -$b + sqrt($b * $b - 4 * $a * $c) / (2 * $a);
}

Calling functions

name(parameterValue, ..., parameterValue);
$x = -2;
$a = 3;
$root = quadratic(1, $x, $a - 2);

Default parameter values

function name(parameterName, ..., parameterName) {
	statements;
}
function print_separated($str, $separator = ", ") {
	if (strlen($str) > 0) {
		print $str[0];
		for ($i = 1; $i < strlen($str); $i++) {
			print $sep . $str[$i];
		}
	}
}
print_separated("hello");        # h, e, l, l, o
print_separated("hello", "-");   # h-e-l-l-o

Variable scope: global and local vars

$school = "UW";                   # global
...

function downgrade() {
	global $school;
	$suffix = "Tacoma";             # local

	$school = "$school $suffix";
	print "$school\n";
}