basic php lecture 17 - hampden-sydney...
TRANSCRIPT
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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