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.
				
				
			 
			
				5.1: Server-Side Basics
				
				
					- 
						5.1: Server-Side Basics
					
 
					- 
						5.2: PHP Basic Syntax
					
 
					- 
						5.3: Embedded PHP
					
 
					- 
						5.4: Advanced PHP Syntax
					
 
				
			 
			
				URLs and web servers
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, and sends back its contents to you
 
						
					 
				
				
			 
			
			
			
			
				Server-Side web programming
				
				
				
				
					- server-side pages are programs written using one of many web programming languages/frameworks
						
					
 
					- the web server contains software that allows it to run those programs and send back their output
 
					- each language/framework has its pros and cons
						
							- we use PHP for server-side programming in this textbook
 
						
					 
				
			 
			
			
			
			
				
					What is PHP?
					(5.1.2)
				
				
				
				
					- PHP stands for "PHP Hypertext Preprocessor"
 
					- a server-side scripting language
 
					- used to make web pages dynamic:
						
							- provide different content depending on context
 
							- interface with other services: database, e-mail, etc
 
							- authenticate users
 
							- process form information
 
						
					 
					- PHP code can be embedded in XHTML code
 
				
			 
			
				
					Lifecycle of a PHP web request
					(5.1.1)
				
				
				
				
					- browser requests a 
.html file (static content): server just sends that file 
					- browser requests a 
.php file (dynamic content): server reads it, runs any script code inside it, then sends result across the network
						
							- script produces output that becomes the response sent back
 
						
					 
				
			 
			
			
			
			
				Why PHP?
				
					There are many other options for server-side languages: Ruby on Rails, JSP, ASP.NET, etc.  Why choose PHP?
				
				
					- free and open source: anyone can run a PHP-enabled server free of charge
 
					- compatible: supported by most popular web servers
 
					- simple: lots of built-in functionality; familiar syntax
 
					- available: installed on UW's servers (Dante, Webster) and most commercial web hosts
 
					- well-documented: type 
php.net/functionName in browser Address bar to get docs for any function 
				
			 
			
				Hello, World!
				
					The following contents could go into a file hello.php:
				
				
<?php
print "Hello, world!";
?>
					
						Hello, world!
					
				 
				
				
					- a block or file of PHP code begins with 
<?php and ends with ?> 
					- PHP statements, function declarations, etc. appear between these endpoints
 
				
			 
			
				Viewing PHP output
				
				
				
					- you can't view your 
.php page on your local hard drive; you'll either see nothing or see the PHP source code 
					- if you upload the file to a PHP-enabled web server, requesting the 
.php file will run the program and send you back its output 
				
			 
			
				5.2: PHP Basic Syntax
				
				
					- 
						5.1: Server-Side Basics
					
 
					- 
						5.2: PHP Basic Syntax
					
 
					- 
						5.3: Embedded PHP
					
 
					- 
						5.4: Advanced PHP 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!
						
					 
				
				
					- some PHP programmers use the equivalent 
echo instead of print 
				
			 
			
				
				
					- 
						
						+ - * / % 
						. ++ -- 
						= += -= *= /= %= .=
						
					 
					- many operators auto-convert types: 
5 + "7" is 12 
				
			 
			
				
$name = expression;
				
$user_name = "PinkHeartLuvr78";
$age = 16;
$drinking_age = $age + 5;
$this_class_rocks = TRUE;
				 
				
					- names are case sensitive; separate multiple words with _
 
					- names always begin with 
$, on both declaration and usage 
					- implicitly declared by assignment (type is not written; a "loosely typed" language)
 
				
			 
			
			
			
			
				
					Comments
					(5.2.7)
				
				
					- like Java, but 
# is also allowed
						
							- a lot of PHP code uses 
# comments instead of // 
							- we recommend 
# and will use it in our examples 
						
					 
				
			 
			
				
					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 (condition) {
	statements;
} elseif (condition) {
	statements;
} else {
	statements;
}
				
					- NOTE: although 
elseif keyword is much more common, else if is also supported 
				
			 
			
				
					while loop
					(same as Java)
				
while (condition) {
	statements;
}
do {
	statements;
} while (condition);