practical php idia 618.185 spring 2012 bridget m. blodgett

15
Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

Upload: jonah-pope

Post on 26-Dec-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

Practical PHP

IDIA 618.185Spring 2012

Bridget M. Blodgett

Page 2: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

$_GET and $_POST

• $_GET and $_POST are system reserved variable names that take the input from a form submission and save the values to an associative array– The index is set to the id of the input

• The variable should be used when the relevant form submission type is included in the HTML page

Page 3: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

$_REQUEST

• $_REQUEST works just like $_GET and $_POST but it will work regardless of the form submission type– It also pulls information from $_COOKIE

• It can be useful if several different forms direct to the same page since only one call is needed to read all the different types of input

Page 4: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

$_COOKIE

• Cookies are a way of identifying users who visit the site and allow for some more dynamic information handling– They can tell when a user has logged in and allow

additional access to that person• setcookie(name, value, expire, path, domain); – Sets a cookie on the user computer (must appear before

the opening <html>• $_COOKIE will retrieve the cookie information

while isset() sees if a cookie has been assigned yet

Page 5: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

printf

• printf controls the format of output by allowing special formatting characters in a string

Specifier Conversion action on argument arg Example (for an arg of 123)% Display a % character (no arg is required) %

b Display arg as a binary integer 1111011

c Display ASCII character for the arg {

d Display arg as a signed decimal integer 123

e Display arg using scientific notation 1.23000e+2

f Display arg as floating point 123.000000

o Display arg as an octal integer 173

s Display arg as a string 123

u Display arg as an unsigned decimal 123

x Display arg in lowercase hexadecimal 7b

X Display arg in uppercase hexadecimal 7B

Table 7-1. The printf conversion specifiers

Page 6: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

printf

• You can have as many specifiers as you like in a printf function,

• Must pass a matching number of arguments• Each specifier is prefaced by a % symbol

printf("My name is %s. I'm %d years old, which is %X in hexadecimal", 'Simon', 33, 33);

Page 7: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

Precision Setting

• PHP allows you to specify how many places appear after the decimal point upon the completion of a calculation

• Place the %.placenumber between the conversion specifier– printf("The result is: $%.2f", 123.42 / 12);

– Example 7-1

Page 8: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

String Precision

• The same concepts work with strings • When specifying padding, if a string is already

of equal or greater length than that value [padding] it will be ignored– unless a cutoff value is given that shortens the

string back to less than the padding value.

Page 9: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

sprintf()

• Allows you to store the value of a string precision specification to another variable for later use or manipulation

$out = sprintf("The result is: $%.2f", 123.42 / 12); echo $out;

Page 10: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

Date and time

• PHP uses standard Unix timestamps– number of seconds since the start of January 1,

1970– Since it is seconds some manipulation is needed to

read the time in a “normal” format• This time next week looks like:– echo time() + 7 * 24 * 60 * 60;

Page 11: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

Review Questions

Page 12: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

Loops

• Using a for loop, write a script that will send to the browser a list of squares for the numbers 1-12.

• Use the format, "1 * 1 = 1", and be sure to include code to print each formula on a different line.

Page 13: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

Switch Statement• Create a form to accept user’s input of the day of the week• Make a switch statement that takes that input and prints the

correct statement– Laugh on Monday, laugh for danger.

Laugh on Tuesday, kiss a stranger. Laugh on Wednesday, laugh for a letter.Laugh on Thursday, something better.Laugh on Friday, laugh for sorrow.Laugh on Saturday, joy tomorrow.

• Remember to include a general response for any input that is not in the poem.

• To make things a little more interesting, include a 'Back' button on the response so that the user can go back and try different days.

Page 14: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

Arrays• Create an array of modes of transportation, including Automobile, Jet,

Ferry, Subway. • Print the following statement to the browser: "Travel takes many forms,

whether across town, across the country, or around the world. Here is a list of some common modes of transportation:“

• Follow this with an unordered list created by iterating through the array variable you created.

• Give the user a text box and ask the user to add other modes of transportation to the list, separated by commas.

• When the user clicks 'Go', process the input with array functions to send back the original list with the user's additions.

• Include another text box with the text "Add more?" and another submit button. When the user clicks this button, the page should reload with the new additions added to the previously expanded list

Page 15: Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

Functions

• Create an array called $months. Use the names of the months as keys, and the number of days for each month as values

• Write a function to create an option element for a form's select field. – Make sure each option will be upper case.– Both the array and the function should precede the HTML for the page.

• Create a form for the user with the request, "Please choose a month." and a select field with the months as options – loop through the array you created and use the function to create the

option elements.• When the user clicks the submit button, return the statement "The

month of $month has $number days