Class07 PHP: loops and includes MIS 3501, Fall 2015 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/15/2015
Course Overview We are here. MySQL To do: HTML & CSS Weeks 1 & 2 PHP Weeks 3 & 4 PDO Week 7 MySQL Weeks 5 & 6 We are here. To do: Organize your code with MVC (week 8) Debug your code (week 9) Work with forms (week 10) Use arrays (week 11 & 12) Use sessions (week 13)
© 2014, Mike Murach & Associates, Inc. What have we learned so far? PHP code runs on the server PHP code is embedded in HTML PHP has a filter_input function that can be used to retrieve data from form posts Like any other programming language, PHP lets us store data in variables Our PHP code can take different actions based on conditional statements © 2014, Mike Murach & Associates, Inc.
Conditional Statements © 2014, Mike Murach & Associates, Inc.
© 2014, Mike Murach & Associates, Inc. A little review… © 2014, Mike Murach & Associates, Inc.
© 2014, Mike Murach & Associates, Inc. A little review… (2) © 2014, Mike Murach & Associates, Inc.
© 2014, Mike Murach & Associates, Inc. A little review… (3) © 2014, Mike Murach & Associates, Inc.
Some more advanced conditional statements Lookie here! – Notice how we are considering three conditions in one statement. This condition reads: “if investment is empty OR investment is not numeric OR investment is less than or equal to 0 THEN … © 2014, Mike Murach & Associates, Inc.
Some more advanced conditional statements(2) Notice also that if statements can be nested inside each other. Here, the “if years greater than one” condition is nested inside the else block of the first condition. Whew! © 2014, Mike Murach & Associates, Inc.
© 2014, Mike Murach & Associates, Inc. This error message was created using a conditional statement. This is just one use of conditional statements. © 2014, Mike Murach & Associates, Inc.
Server-Side Includes (Some assembly required) © 2014, Mike Murach & Associates, Inc.
© 2014, Mike Murach & Associates, Inc. Built-in functions used to assemble content from different files. The include and require functions are similar. They both inject content from another file into the current PHP page. The require function is the more strict of the two. If the file is not found, an warning is displayed and further processing of the page stops. Include let’s processing continue, but with a warning. These functions are just like their cousins above. They silently prevent the same content from being injected over and over again. The exit and die functions do exactly the same thing: completely terminate further processing of the php page. © 2014, Mike Murach & Associates, Inc.
Moving up and down the hierarchy of our server’s file system Sometimes we choose to assemble our page based on one or more conditions. Moving up and down the hierarchy of our server’s file system © 2014, Mike Murach & Associates, Inc.
Loops (Doing the same thing over and over again) © 2014, Mike Murach & Associates, Inc.
© 2014, Mike Murach & Associates, Inc. A loop that stores the numbers 1 through 5 in a string © 2014, Mike Murach & Associates, Inc.
© 2014, Mike Murach & Associates, Inc.
© 2014, Mike Murach & Associates, Inc.
The Future Value Calculator uses a loop © 2014, Mike Murach & Associates, Inc.
∑f(xi) where f(x) = x + x*i How was this number calculated? ∑f(xi) where f(x) = x + x*i © 2014, Mike Murach & Associates, Inc.