Basic PHP Lecture 17

Similar documents
Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

LR Parsing - Conflicts

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

The Critical-Path Algorithm

Programming Languages

The Pairwise-Comparison Method

Minimal Spanning Trees

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The Traveling Salesman Problem Brute Force Method

PHP Queries and HTML Forms Lecture 23

JFlex Regular Expressions

Scope and Parameter Passing

Basic HTML. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 20, 2013

Friends and Unary Operators

Basic CSS Lecture 17

Programming Languages

Solving Recursive Sequences by Iteration

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Basic HTML. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 20, 2013

The Class Construct Part 1

The Plurality-with-Elimination Method

Fundamental Data Types

Rotations and Translations

Basic HTML Lecture 14

Magnification and Minification

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Scope and Parameter Passing

Scheduling and Digraphs

The Decreasing-Time Algorithm

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Displaying Distributions - Quantitative Variables

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

LR Parsing - The Items

Relational Databases Lecture 2

Introduction to Databases

JFlex. Lecture 16 Section 3.5, JFlex Manual. Robb T. Koether. Hampden-Sydney College. Mon, Feb 23, 2015

Sampling Distribution Examples Sections 15.4, 15.5

Recursive Linked Lists

Density Curves Sections

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

The Graphics Pipeline

Relational Databases

Implementing Linked Lists

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Introduction to Databases

Recursive Descent Parsers

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

The Normal Distribution

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Recognition of Tokens

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

XQuery FLOWR Expressions Lecture 35

Ambient and Diffuse Light

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Javascript Lecture 23

Lecture 12. PHP. cp476 PHP

List Iterator Implementation

Street-Routing Problems

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

MySQL Creating a Database Lecture 3

XML. Lecture 29. Robb T. Koether. Fri, Mar 28, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XML Fri, Mar 28, / 23

Introduction to Compiler Design

The CYK Parsing Algorithm

The Coefficient of Determination

Dynamic Allocation of Memory

The Projection Matrix

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

The x86 Architecture

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

The Graphics Pipeline

XML and AJAX Lecture 28

Nondeterministic Programming in C++

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Binary Tree Applications

Stacks and their Applications

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The x87 Floating-Point Unit

Building the Abstract Syntax Trees

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

PHP by Pearson Education, Inc. All Rights Reserved.

Transcription:

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