basic php lecture 17 - hampden-sydney...

32
Basic PHP Lecture 17 Robb T. Koether Hampden-Sydney College Fri, Feb 24, 2012 Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 1 / 30

Upload: others

Post on 24-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Basic PHPLecture 17

Robb T. Koether

Hampden-Sydney College

Fri, Feb 24, 2012

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 1 / 30

Page 2: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

1 PHP

2 Basic PHP

3 The Extended echo Statement

4 Variables

5 Operators

6 Formatted Output

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 2 / 30

Page 3: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Outline

1 PHP

2 Basic PHP

3 The Extended echo Statement

4 Variables

5 Operators

6 Formatted Output

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 3 / 30

Page 4: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

PHP

PHP = “PHP Hypertext Preprocessor”

Get it?PHP code is placed in a .php file.Or is can be embedded in an .html file.When the client requests a file containing PHP code, the server

Executes the PHP code.Sends the output of the PHP code to the client.

Typically, the output of the PHP program is HTML code andJavascript code.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 4 / 30

Page 5: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

PHP

PHP = “PHP Hypertext Preprocessor”Get it?

PHP code is placed in a .php file.Or is can be embedded in an .html file.When the client requests a file containing PHP code, the server

Executes the PHP code.Sends the output of the PHP code to the client.

Typically, the output of the PHP program is HTML code andJavascript code.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 4 / 30

Page 6: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

PHP

PHP = “PHP Hypertext Preprocessor”Get it?PHP code is placed in a .php file.Or is can be embedded in an .html file.When the client requests a file containing PHP code, the server

Executes the PHP code.Sends the output of the PHP code to the client.

Typically, the output of the PHP program is HTML code andJavascript code.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 4 / 30

Page 7: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

PHP

PHP Code<?phpPHP_code?>

PHP code is opened by <?php and is closed by ?>.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 5 / 30

Page 8: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Outline

1 PHP

2 Basic PHP

3 The Extended echo Statement

4 Variables

5 Operators

6 Formatted Output

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 6 / 30

Page 9: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The echo Statement

The echo Statementecho expression;

The echo statement will output the value of an expression.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 7 / 30

Page 10: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The echo Statement

The echo Statement<?phpecho "Hello, world!";?>

For example, the above code will output Hello, world!.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 8 / 30

Page 11: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Mixing PHP and HTML

Mixing PHP and HTML<html><head><?phpecho "<title>My Web Page</title>";?></head><body><?php echo "<h1>My Web Page</h1></body></html>" ?>

Mixing PHP and HTML can be ugly, but it works.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 9 / 30

Page 12: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Strings

Strings and Quotation Marks<?phpecho "Hello, ’world!’";echo ’Hello, "world!"’;?>

Strings may be delimited by either single quotes (’) or doublequotes (").Single quotes may be embedded within double quotes, and viceversa.The above code will output Hello, ’world!’ and Hello,"world!".

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 10 / 30

Page 13: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The echo Statement

Concatenationecho "Hello," . " world!";

The dot (.) is the string concatenation operator.The above code will output

Hello, world!

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 11 / 30

Page 14: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The echo Statement

Numerical Expressionsecho 123;echo 123 + 456;echo 123 - 456;echo 123 * 456

Numerical expressions in an echo statement are evaluated andthen the value is outputThe above code will output

123579-33356088

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 12 / 30

Page 15: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The echo Statement

Mixing Typesecho "Hello, " . 2011 . " world!"

When numerical values are output, they are first converted tostrings.Therefore, we may concatenate strings and numbers.The above code will output

Hello, 2011 world!

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 13 / 30

Page 16: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The echo Statement

Linebreaksecho 123 . ’<br/>’;echo 123 + 456 . ’<br/>’;echo 123 * 456 . ’<br/>’;echo "123 * 456";

The output of the echo statement is interpreted by the browser asHTML code.Thus, to insert line breaks in the output, we must concatenate theoutput with the string ’<br/>’.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 14 / 30

Page 17: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Outline

1 PHP

2 Basic PHP

3 The Extended echo Statement

4 Variables

5 Operators

6 Formatted Output

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 15 / 30

Page 18: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The Extended echo Statement

The Extended echo Statementecho <<<_HTMLlong section of text_HTML;

Sometimes we want to echo very long string.We may use whatever delimiter we like. I suggest _HTML becausethis is used typically to output HTML code.Terminating delimiter must begin in column 1 and there must benothing else on that line.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 16 / 30

Page 19: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The Extended echo Statement

The Extended echo Statementecho <<<_HTML<html><head><title>My Web page</title><style>h1 {text-align: center; color: darkgreen;}</style><head><body>_HTML;

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 17 / 30

Page 20: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Outline

1 PHP

2 Basic PHP

3 The Extended echo Statement

4 Variables

5 Operators

6 Formatted Output

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 18 / 30

Page 21: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Variables

Assignment Statements and Data Types$x = "Hello"; // String type$x = 44 + 55; // Integer type$x = $x . " bottles of beer on the wall"; // What type?echo $x;

Variable names being with $.A variable’s type can change.A variable’s current type is determined by its current value.When the value changes, the type may change.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 19 / 30

Page 22: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Variables

Variables Within Strings$x = 44 + 55;echo "$x bottles of beer on the wall";echo ’$x bottles of beer on the wall’;

What will the above code produce?

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 20 / 30

Page 23: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Outline

1 PHP

2 Basic PHP

3 The Extended echo Statement

4 Variables

5 Operators

6 Formatted Output

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 21 / 30

Page 24: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Operators

PHP operatorsThe assignment operator: =Numerical operators: +, -, *, /, %, ++, --String operators: .Comparison operators: ==, !=, <, >, <=, >=, ===, !==

The expression $a === $b is true if $a and $b have the samevalue and are of the same type.The expression $a !== $b is true if $a and $b have differentvalues or are of different types.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 22 / 30

Page 25: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Operators

Mixing Types and Operatorsecho "123" == "123";echo 123 == 123;echo "123" == 123;echo 123.0 == 123;echo ’123’ == "123";echo (123 . "") == "123";

These expressions all evaluate to true.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 23 / 30

Page 26: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Operators

Mixing Types and Operatorsecho "123" === "123";echo 123 === 123;echo "123" === 123;echo 123.0 === 123;echo ’123’ === "123";echo (123 . "") === "123";

What about these?

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 24 / 30

Page 27: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Operators

Mixing Types and Operatorsecho "123" + "456" . ’<br/>’;echo "123" . "456" . ’<br/>’;echo "Hello" + "123";

Data types in PHP are very fluid, but can produce surprisingresults.Test the above code.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 25 / 30

Page 28: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Debugging Programs

Inspecting Variablesecho ’The value of $x is ’ . $x . ’<br/>’;

The most common method of debugging a PHP program is toinspect the values of variables.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 26 / 30

Page 29: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Outline

1 PHP

2 Basic PHP

3 The Extended echo Statement

4 Variables

5 Operators

6 Formatted Output

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 27 / 30

Page 30: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The printf() Function

The printf() Functionprintf(format_string, list_of_variables)

We may use the printf() function to display formatted output.This function is “borrowed” directly from C.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 28 / 30

Page 31: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

The printf() Function

In the format string,%nd for integers, where n = field width.%n.df for floats, where n = field width and d = number of decimalplaces.%s for strings.

All other values in the format string are interpreted literally.

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 29 / 30

Page 32: Basic PHP Lecture 17 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · 1 PHP 2 Basic PHP 3 The Extended echo Statement 4 Variables 5

Formatted Output

The printf() Function$count = 400;$price = 23.4;$desc = "paper cups";printf("Order: %d of %s at $%6.2f per item; total = $%8.2f",$count, $desc, $price, $count*$price);

The above code will produce

Order: 400 of paper cups at $ 23.40 per item; total = $ 9360.00

Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 30 / 30