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
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 Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 4 / 30
PHP PHP = PHP Hypertext Preprocessor Get it? 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 and Javascript code. Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 4 / 30
PHP PHP Code <?php PHP_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 Statement echo 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 <?php echo "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> <?php echo "<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 <?php echo "Hello, world! "; echo Hello, "world!" ;?> Strings may be delimited by either single quotes ( ) or double quotes ("). Single quotes may be embedded within double quotes, and vice versa. 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 Concatenation echo "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 Expressions echo 123; echo 123 + 456; echo 123-456; echo 123 * 456 Numerical expressions in an echo statement are evaluated and then the value is output The 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 Types echo "Hello, ". 2011. " world!" When numerical values are output, they are first converted to strings. 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 Linebreaks echo 123. <br/> ; echo 123 + 456. <br/> ; echo 123 * 456. <br/> ; echo "123 * 456"; The output of the echo statement is interpreted by the browser as HTML code. Thus, to insert line breaks in the output, we must concatenate the output 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 Statement echo <<<_HTML long section of text _HTML; Sometimes we want to echo very long string. We may use whatever delimiter we like. I suggest _HTML because this is used typically to output HTML code. Terminating delimiter must begin in column 1 and there must be nothing 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 Statement echo <<<_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 operators The assignment operator: = Numerical operators: +, -, *, /, %, ++, -- String operators:. Comparison operators: ==,!=, <, >, <=, >=, ===,!== The expression $a === $b is true if $a and $b have the same value and are of the same type. The expression $a!== $b is true if $a and $b have different values or are of different types. Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 22 / 30
Operators Mixing Types and Operators echo "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 Operators echo "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 Operators echo "123" + "456". <br/> ; echo "123". "456". <br/> ; echo "Hello" + "123"; Data types in PHP are very fluid, but can produce surprising results. Test the above code. Robb T. Koether (Hampden-Sydney College) Basic PHPLecture 17 Fri, Feb 24, 2012 25 / 30
Debugging Programs Inspecting Variables echo The value of $x is. $x. <br/> ; The most common method of debugging a PHP program is to inspect 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() Function printf(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 decimal places. %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