Lecture 7 
 Embedded PHP
				Reading: 5.3 - 5.5
				
					Except where otherwise noted, the contents of this presentation are Copyright 2010 Marty Stepp and Jessica Miller.
				
				
			 
			
				5.2: PHP Basic Syntax
				
				
					- 
						5.1: Server-Side Basics
					
 
					- 
						5.2: PHP Basic Syntax
					
 
					- 
						5.3: Embedded PHP
					
 
					- 
						5.4: Advanced PHP Syntax
					
 
				
			 
			
				
					PHP syntax template
				
HTML content
	<?php
		PHP code
	?>
HTML content
	<?php
		PHP code
	?>
HTML content ...
				
					- any contents of a 
.php file between <?php and ?> are executed as PHP code 
					- all other contents are output as pure HTML
 
					- can switch back and forth between HTML and PHP "modes"
 
				
			 
			
				
$a = 3;
$b = 4;
$c = sqrt(pow($a, 2) + pow($b, 2));
				
				
					math constants
					
							
							M_PI
						 | 
						
							M_E
						 | 
						
							M_LN2
						 | 
					
				
				
				
					- the syntax for method calls, parameters, returns is the same as Java
 
				
			 
			
				int and float types
$a = 7 / 2;               
$b = (int) $a;            
$c = round($a);           
$d = "123";               
$e = (int) $d;            
				
					int for integers and float for reals 
					- division between two 
int values can produce a float 
				
			 
			
				
					String type
					(5.2.6)
				
$favorite_food = "Ethiopian";
print $favorite_food[2];            
				
					- zero-based indexing using bracket notation
 
					- string concatenation operator is 
. (period), not +
						
							5 + "2 turtle doves" produces 7 
							5 . "2 turtle doves" produces "52 turtle doves" 
						
					 
					- can be specified with 
"" or '' 
				
			 
			
				Interpreted strings
$age = 16;
print "You are " . $age . " years old.\n";
print "You are $age years old.\n";    
				
			 
			
				String functions
$name = "Stefanie Hatcher";
$length = strlen($name);              
$cmp = strcmp($name, "Brian Le");     
$index = strpos($name, "e");          
$first = substr($name, 9, 5);         
$name = strtoupper($name);            
				
			 
			
				
					bool (Boolean) type
					(5.2.8)
				
$feels_like_summer = FALSE;
$php_is_rad = TRUE;
$student_count = 217;
$nonzero = (bool) $student_count;     
				
					- the following values are considered to be 
FALSE (all others are TRUE):
						
							- 
								
0 and 0.0
							 
							- 
								
"", "0", and NULL (includes unset variables) 
							- arrays with 0 elements
 
						
					 
					- can cast to boolean using 
(bool) 
					FALSE prints as an empty string (no output); TRUE prints as a 1 
				
				
				
					
						TRUE and FALSE keywords are case insensitive 
					
				 
			 
			
			
				5.3: Embedded PHP
				
				
					- 
						5.1: Server-Side Basics
					
 
					- 
						5.2: PHP Basic Syntax
					
 
					- 
						5.3: Embedded PHP
					
 
					- 
						5.4: Advanced PHP Syntax
					
 
				
			 
			
			
			
			
				Printing HTML tags in PHP = bad style
				
<?php
print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n";
print " \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n";
print "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
print "  <head>\n";
print "    <title>Geneva's web page</title>\n";
...
for ($i = 1; $i <= 10; $i++) {
	print "<p> I can count to $i! </p>\n";
}
?>
				
				
					- printing HTML tags with 
print statements is bad style and error-prone:
						
							- must quote the HTML and escape special characters, e.g. 
\" 
						
					 
					- but without 
print, how do we insert dynamic content into the page? 
				
			 
			
				
					PHP expression blocks
					(5.3.2)
				
<?= expression ?>
				
<h2> The answer is <?= 6 * 7 ?> </h2>
					
						
The answer is 42
					
				 
				
					- 
						PHP expression block: evaluates and embeds an expression's value into HTML
					
 
					- 
						
<?= expr ?>    is equivalent to    <?php print expr; ?>
					 
				
			 
			
			
			
			
				Expression block example
				
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head><title>CSE 190 M: Embedded PHP</title></head>	
	<body>
		<?php for ($i = 99; $i >= 1; $i--) { ?>
			<p> <?= $i ?> bottles of beer on the wall, <br />
				  <?= $i ?> bottles of beer. <br />
				  Take one down, pass it around, <br />
				  <?= $i - 1 ?> bottles of beer on the wall. </p>
		<?php } ?>
	</body>
</html>
			 
			
				Common errors: unclosed braces, missing = sign
				
	<body>
		<p>Watch how high I can count:
			<?php for ($i = 1; $i <= 10; $i++) { ?>
				<? $i ?>
		</p>
	</body>
</html>
				
			 
			
				Complex expression blocks
				
				
	<body>
		<?php for ($i = 1; $i <= 3; $i++) { ?>
			<h<?= $i ?>>This is a level <?= $i ?> heading.</h<?= $i ?>>
		<?php } ?>
	</body>
					
					
						
This is a level 1 heading.
						This is a level 2 heading.
						This is a level 3 heading.
					
				 
				
				
					- expression blocks can even go inside HTML tags and attributes