Web Programming Step by Step

Lecture 6-B
Introduction to PHP

Reading: 5.1 - 5.2

Except where otherwise noted, the contents of this presentation are Copyright 2010 Marty Stepp and Jessica Miller.

Valid XHTML 1.1 Valid CSS!

5.1: Server-Side Basics

URLs and web servers

http://server/path/file

Server-Side web programming

php jsp ruby on rails asp.net

What is PHP? (5.1.2)

PHP logo

Lifecycle of a PHP web request (5.1.1)

PHP server

Why PHP?

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

Hello, World!

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

<?php
print "Hello, world!";
?>
Hello, world!

Viewing PHP output

PHP local output PHP server output

5.2: PHP Basic Syntax

Console output: print (5.2.2)

print "text";
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!';
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!

Arithmetic operators (5.2.4)

Variables (5.2.5)

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

Types (5.2.3)

Comments (5.2.7)

# single-line comment

// single-line comment

/*
multi-line comment
*/

String type (5.2.6)

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

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) (5.2.9)

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

if/else statement

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

while loop (same as Java)

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