Lab 1:

Covers weeks 1 to 4 materials. 

For everybody's info, lab 1 problems cover materials from weeks 1 to 4.  So after the first week, we have not covered the materials needed to do most of the problems.  But you can do Problem #1.  And as we progress through weeks 2, 3 and 4, we should be able to do all the problems in Lab 1.

Please do not submit your work until you have completed all of the problems listed below.

How to Submit:

Create a Word document, paste the source code, capture the image of the output and paste it to the Word document, and submit the Word document as an attachment in Etudes-NG.   

Word attachment:  Submit the codes and the output only. Label your work as follows:

**************************************************************

Problem 1:

(the codes)

(the output)

*************************************************************

Problem 2:

(the codes)

(the output)

etc.

etc.

**************************************************************

Note: Please include the rows of asterisks to separate each problem. Failure to follow the above instructions will result in a deduction of 4 points from your lab work.

************************************************************************

Problems:

1.  Create a Web page that contains a "here document" block.  Please make up your own text in the "here document" block. 

Note:  Please refer to:

http://hudzilla.org/phpwiki/index.php?title=Heredoc_syntax

 

http://books.google.com/books?id=mPzUH1EPEJkC&pg=PA64&dq=here+document+in+PHP#PPA64,M1

http://www.phpf1.com/tutorial/php-heredoc-syntax.html

http://books.google.com/books?id=PVvmMRSGzFEC&pg=PA19&lpg=PA19&dq=here+document+in+PHP&source=web&ots=u3zvFoJuWB&sig=FdVkyMGIcnELWhVg5ZeyuyzvPLY&hl=en&sa=X&oi=book_result&resnum=5&ct=result

2.  Write a script that declares and assigns five different variables.   Populate them with values of different data types and use the gettype ( ) function to print each type to the browser.   

3.  Write a script that declares and assigns floating-point variables for the interest rate you are paying on your car, credit card, mortgage, student loan, or some other type of loan.  Create floating-point variables for at least three types of loans.  If you do not have any type of loan with an interest rate, look in your local newspaper or on the Internet for current interest rates for morgages and autos.  Use echo ( ) statements to print each variable, along with a description of the loan, and the gettype ( ) function to make sure each variable is of the double data type.  

4.  Write an HTML form (see NOTE below -- end of this page on PHP Forms and User Input) that prompts the user to enter a value.  In PHP, write a script that uses conditional operator to determine whether a variable contain a number and whether the number is even.  You need to use the is_numeric ( ) function and the conditional operator.  For floating-point numbers, you need to use the round ( ) function to convert the value to the nearest whole number.   Code this program using only conditional operator.

Test the program with the following values:

1.  a non-numeric value

2.  an even #

3.  an odd #

4.  a number with decimal points

 Please submit four output screenshots of the results -- one for each:

1.  a non-numeric value

2.  an even #

3.  an odd #

4.  a number with decimal points

 Notes on Problem #4: 

The Ternary Operator is a comparison operator commonly found in other programming languages including PHP. It can end up saving you a bit of time and lines of code.

Here is the Format: ( expr1 ) ? ( expr2 ) : (expr3)

If expr1 is true, then it evaluates to ( expr2 )
If expr1 is false, then it evaluates to ( expr3 )

 Here's an example:

<?php
$agestr = ($age < 16) ? 'child' : 'adult';
?>

First there is a condition ($age < 16), then there is a question mark, and then a true result, a colon, and a false result. If $age is less than 16, $agestr will be set to 'child', otherwise it will be set to 'adult'. That one-liner ternary statement can be expressed in a normal if statement like this:

if ($age < 16) {
$agestr = 'child';
} else {
$agestr = 'adult';
}
?>

So, in essence, using the ternary operator allows you to compact five lines of code into one, at the expense of some readability.

Nesting Conditionals:

This form can be quite readable:

    result = (foo == bar)  ? result1 :
(foo == baz)  ? result2 :
(foo == qux)  ? result3 :
(foo == quux) ? result4 :
fail_result;

The condition and the result are kept together on the same line, and it's fairly easy to skim down and understand what's going on.

Please check out:  http://php.dzone.com/news/tips-tricks-learning-ternary-o
http://17thdegree.com/archives/2008/01/09/php-and-nesting-ternary-operators/
http://franktank.com/blog/web-development-tips/php-ternary-syntax/
http://davidwalsh.name/learning-ternary-operators-tips-tricks

PHP Forms Resources:

http://www.w3schools.com/php/php_forms.asp

http://www.phpf1.com/tutorial/php-form.html

5.  Use the while( ) loop to write a script that prints a list of the Celsius equivalents of zero degrees Fahrenheit through 100 degrees Fahrenheit.  To convert Fahrenheit to Celsuis, subtract 32 from the Fahrenheit temperature, and then multiply the remainder by .55.  To convert Celsius to Fahrenheit, multiply the Celsius temperature by 1.8, and then add 32. You may use any increment of temperature.

6.  Modify Problem #5 to use for( ) loop.

7.  Write an HTML form that prompts the user for his or her grade on three different tests. Grades range from 0 to 100.

Write a PHP program that tests that the user typed in a numeric grade (see is_numeric()). Average the three grades using the following scale. Use the if/elseif conditional construct to determine the student's letter grade.

90–100 = A

80–89 = B

70–79 = C

60–69 = D

0–59 = F

Display the average and the letter grade in the browser. If the student got an A, print the output using an HTML heading level 1 <H1>. If he or she got a B, do the same, but decrease the heading level to <H2> and continue this for each lower grade. If he or she received an F score, send a message such as, "You need to concentrate!" in a red <H5> tag.

8.  Click here for problem 8.

Reference:  http://www.w3schools.com/php/func_math_rand.asp

9.  Create a form that asks a user to select a destination from a list of radio buttons. The choices are: San Francisco, New York, London, Paris, Honolulu, and Tokyo.

Write a PHP script that uses a switch statement to evaluate each of the cities, and based on the city selected by the user, sends back a message such as, "Welcome to San Francisco. Be sure to bring your heart and a jacket!" or "Bonjour, let's take a stroll on the left bank!".

For Problem 9:  Please have two files:  one HTML and one PHP

10.  Ask the user how many Euros he or she spent in Germany? Write a function to convert the Euros to U.S. dollars based on today's exchange rate.

11. Create a function called converter() that will take two arguments. The first argument will be upper, lower, or title. The second argument will be a string. The function should display the string in all uppercase, all lowercase, or as a title (that is, where the first letter in each word is uppercase as in, "Having Fun With Dick and Jane"). The function will be called like this:

 print converter("title", $string);

Click Here to see the output.

 Side note:  You don't need to use forms in Lab 1 Problem 11.

**********************************************************************************

NOTE:  Please read:  PHP Forms and User Input

The $_SERVER['PHP_SELF'] or old syntax $PHP_SELF superglobal gives you the filename of the currently executing script, relative to the document root.  Often used in forms.  $PHP_SELF is deprecated.

Please check:

http://answers.yahoo.com/question/index?qid=20080725052910AAK7cXM

http://www.weberdev.com/ViewArticle/Custom-Error-Messages-When-Using-$PHP_SELF-as-Form-Action