CSSE 290 Web Programming

Lecture 6: Intro to PHP

Reading: 5.1 - 5.2

Attribution:Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.

Otherwise noted: Claude Anderson was given permission to modify the slides for CSSE 290 at Rose-Hulman by author Jessica Miller. The authors' original slides, based on Web Programming Step by Step, can be seen at http://webstepbook.com.
Some of the examples in some days' slides are from David Fisher at Rose-Hulman, who was kind enough to allow me to use them. My intention is to mark these examples with [DSF].

Valid HTML Valid CSS!

Announcements and setup

5.1: Server-Side Basics

URLs and Web Servers

http://server/path/file

Server-Side Web Programming

php jsp ruby on rails asp.net django nodejs

What is PHP?

PHP logo

Lifecycle of a PHP Web Request

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!

Hello, World! HTML

<!DOCTYPE html>
<html>
  <head>
    <title>Hello world</title>
    <meta http-equiv="Content-Type"
      content="text/html; charset=iutf-8" />
    </head>
  <body>

    <?php
       print "Hello, World";
    ?>

  </body>
</html>
  • When a client views the source, only HTML is visible
  • Viewing PHP Output

    PHP local output PHP server output

    5.2: PHP Basic Syntax

    System Information: phpinfo()

    <?php
    phpinfo();
    ?>
    

    Console Output: print

    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

    Variables

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

    Types

    Comments

    # single-line comment
    
    // single-line comment
    
    /*
    Multi-line Comment
    */
    

    for loop

    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);
    

    Math Functions

    $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

    Altogether, here are about 50 predefined math functions and 23 constants

    math constants
    M_PI M_E M_LN2

    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
    

    String type

    $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.
    

    String Functions

    # index  0123456789012345
    $name = "Stefanie Hatcher";
    $length = strlen($name);              # 16
    $cmp = strcmp($name, "Brian Le");     # > 0
    $index = strpos($name, "e");          # 2
    $first = substr($name, 9, 5);         # "Hatch"
    $name = strtoupper($name);            # "STEFANIE HATCHER"
    
    NameJava Equivalent
    strlen length
    strpos indexOf
    substr substring
    strtolower, strtoupper toLowerCase, toUpperCase
    trim trim
    explode, implode split, join
    strcmp compareTo

    bool (Boolean) Type

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

    Functions

    function name(parameterName, ..., parameterName) {
    	statements;
    }
    
    function bmi($weight, $height) {
    	$result = 703 * $weight / $height / $height;
    	return $result;
    }
    

    Calling Functions

    name(expression, ..., expression);
    
    $w = 163;  # pounds
    $h = 70;   # inches
    $my_bmi = bmi($w, $h);
    

    5.3: Embedded PHP

    Printing HTML Tags in PHP = Bad Style

    <?php
    print "<!DOCTYPE html>\n";
    print "<html>\n";
    print "  <head>\n";
    print "    <title>Geneva's web page</title>\n";
    ...
    for ($i = 1; $i <= 10; $i++) {
    	print "<p class=\"count\"> I can count to $i! </p>\n";
    }
    ?>
    

    PHP Syntax Template

    HTML content
    
    	<?php
    	PHP code
    	?>
    
    HTML content
    
    	<?php
    	PHP code
    	?>
    
    HTML content ...
    

    PHP expression blocks

    <?= expression ?>
    
    <h2> The answer is <?= 6 * 7 ?> </h2>
    

    The answer is 42

    Expression Block Example

    <!DOCTYPE html>
    <html>
    	<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.

    Let's Do an Example Together

    PascalTriangle