Web Programming Step by Step

Lecture 6
Introduction to PHP

Reading: 5.1 - 5.3

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

Valid XHTML 1.1 Valid CSS!

4.4: Sizing and Positioning

The display property (4.4.4)

h2 { display: inline; background-color: yellow; }

This is a heading

This is another heading

property description
display sets the type of CSS box model an element is displayed with

Displaying block elements as inline

<ul id="topmenu">
	<li>Item 1</li>
	<li>Item 2</li>
	<li>Item 3</li>
</ul>
#topmenu li {
	display: inline;
	border: 2px solid gray;
	margin-right: 1em;
}

The visibility property

p.secret {
	visibility: hidden;
}

Since nobody can see this anyway: ca-ca poo-poo pee-pee!!!

property description
visibility sets whether an element should be shown onscreen;
can be visible (default) or hidden

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!

Variables (5.2.5)

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

Types (5.2.3)

Arithmetic operators (5.2.4)

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";
}

bool (Boolean) type (5.2.8)

$feels_like_summer = FALSE;
$php_is_rad = TRUE;

$student_count = 217;
$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)

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

Math operations

$a = 3;
$b = 4;
$c = sqrt(pow($a, 2) + pow($b, 2));
math functions
abs ceil cos floor log log10 max
min pow rand round sin sqrt tan
math constants
M_PI M_E M_LN2