Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett.

Similar presentations


Presentation on theme: "Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett."— Presentation transcript:

1 Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett

2 $_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

3 $_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

4 $_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 $_COOKIE will retrieve the cookie information while isset() sees if a cookie has been assigned yet

5 printf printf controls the format of output by allowing special formatting characters in a string SpecifierConversion action on argument argExample (for an arg of 123) %Display a % character (no arg is required)% bDisplay arg as a binary integer1111011 cDisplay ASCII character for the arg{ dDisplay arg as a signed decimal integer123 eDisplay arg using scientific notation1.23000e+2 fDisplay arg as floating point123.000000 oDisplay arg as an octal integer173 sDisplay arg as a string123 uDisplay arg as an unsigned decimal123 xDisplay arg in lowercase hexadecimal7b XDisplay arg in uppercase hexadecimal7B Table 7-1. The printf conversion specifiers

6 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);

7 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

8 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.

9 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;

10 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;

11 Review Questions

12 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.

13 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.

14 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

15 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


Download ppt "Practical PHP IDIA 618.185 Spring 2012 Bridget M. Blodgett."

Similar presentations


Ads by Google