Thesis

Submitted by: Submitted by

Views: 10

Words: 1825

Pages: 8

Category: Other Topics

Date Submitted: 07/12/2015 09:39 PM

Report This Essay

6.470 IAP 2013

PHP Exercises

Here are some exercises on PHP. The first two are simply to get you used to the syntax of PHP,

and the last three go over many server-side programming issues that you will encounter

frequently. Each exercise tells you when you should be able to successfully complete the

exercise. If you encounter problems, our best suggestion is to first look at lecture notes, and then

consult the PHP manual at php.net.

In the first two exercises, you are asked to write a function first. You can check yourself by

calling the function in the script after it is declared. If the file is named filename.php, simply type

‘php filename.php’ on an Athena or Mac OSX terminal, and observe the output. For example:

The file hello.php:

On the terminal:

$ php hello.php

hello

Afterwards, you will need to use the function, in conjunction with other code and HTML, to get

the desired output on a webpage.

Exercise 1: Charlie bit my finger! (after lecture 3)

Part 1: Charlie will bite your finger exactly 50% of the time. First, write a function

isBitten() that returns TRUE with 50% probability, and FALSE otherwise.

Hint: You may find the rand() function useful.

Part 2: Below the function, write code to generate a webpage that displays “Charlie bit your

finger!” or “Charlie did not bite your finger!” using the isBitten() function.

Exercise 2: Counting words (after lecture 6)

Part 1: Write a function countWords($str) that takes any string of characters and finds the

number of times each word occurs. You should ignore the distinction between capital and

lowercase letters, and do not have to worry about dealing with characters that are not letters.

Hint: Create an associative array mapping word keys to the number of times they occur. You will

need to look at PHP's string functions to split a sentence into words.

Hint 2: The print_r($array_name) function is useful for examining the contents of an

array.

Part 2: In the same file, write a form...