Codingassignmenthelper | Home Codingassignmenthelper | Samples

ITECH3108 Dynamic Web Development Exercise

ITECH3108

ITECH3108 Dynamic Web Development Exercise

		ITECH3108 Dynamic Web Development
Lab exercise – Strings, Functions and Arrays
The objective of this exercise is to continue to develop your understanding of PHP code.
By the end of this exercise, you should feel confident to:


Search for individual functions in the PHP documentation



Read and understand basic PHP code



Use PHP functions to manipulate strings



Use loops to extract information from arrays

Prerequisites
If Apache is not running on your machine start the server through XAMPP or WAMPServer. Create your own
directory under the Apache document root (see lab 1 for details).
For this lab exercise you need to go to the http://php.net website, however you do not need to download the
documentation.

String functions
All string functions manipulate strings in various ways. The purpose of this exercise is to make you familiar
with String functions.
Point your web browser to http://php.net
Search for a function called strlen. The search engine should display page for the strlen() function.

Task 1
Read the manual about the strlen() function.
□

What does this function return?

□

What does it expect as a parameter?

Read examples about using strlen() function.
Create a PHP page that uses the strlen() function to display the length of a string containing your
first name and surname (e.g $myname = “Cameron Foale”; ).

Task 2
Search for the wordwrap() function. What does this function return? Read on the examples provided at the
bottom of wordwrap() page.
Write a PHP code that uses this function in order to wrap string "The quick brown dog jumped over
the lazy cow." to 10 characters, use “
” as string break and display it to the browser. CRICOS Provider No. 00103D ITECH3108 Lab 2 - Strings, Arrays and Loops.docx Page 1 of 5 Task 3 Examine the following sequence of code and predict what it will do. Implement it to check your answer. Note the exclamation mark ! before strcmp(), what does it do? (Hint, what does strcmp return?) $a = "apple"; $b = "banana"; if (!strcmp($a, $b)) { print "$a"; } else { print "$b"; } Task 4 Repeat your search this time for substr() function in the function list. As we can see substr() returns part of a string (substring). What does this function return? What does it expect as a parameter? Read on examples about using substr() function. Write PHP code that uses substr() function with string "I love bananas" as a parameter and displays the following text to the browser: 1. I love 2. ana 3. love 4. bananas 5. BANANAS *Hint: use strtoupper() Arrays and looping (Sequential array) For each task assume an array called $months has been declared as follows: $months = array ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); Task 5 1. Write a 'for' loop that will print names of last four months (Sep-Dec). 2. Write a 'for' loop that will print names of every second month (*Hint use %). 3. Write a 'foreach' loop that will print names of every month and its index. 4. Write a 'foreach' loop that will print names of every month and its index. The loop should terminate when the index is 8. 5. Write a 'foreach' loop that will print names of every month and its index. The loop should terminate when the array value is 'Aug'. CRICOS Provider No. 00103D ITECH3108 Lab 2 - Strings, Arrays and Loops.docx Page 2 of 5 Arrays and looping (Associative array) For each task assume an array called $days_per_month has been declared as follows: $days_per_month = array( 'Jan'=>31,'Feb'=>28,'Mar'=>31,'Apr’=>30, 'May'=>31,'Jun'=>30,'July'=>31,'Aug'=>31, 'Sep'=>30,'Oct'=>31,'Nov'=>30,'Dec'=>31 ); Task 6 1. Write a 'foreach' loop that will output month name and number of days foreach element in the $days_per_month array (Note each print should have break
and new line \n) 2. Write PHP code that displays the total number of days in the $days_per_month array. *Hint – use array_values( ) and array_sum( ) functions. 3. Write PHP code that will display a line containing all array keys from the $days_per_month array *Hint – use array_keys( ) function 4. Write PHP code that outputs to the browser: "This is an array" if $days_per_month is variable of type "array" otherwise "This is not an array". Show the code and output for the above 6 tasks, together with code for the String functions, to your tutor to get one mark for this lab. You must be prepared to answer questions about your work. Additional tasks (optional) Note: the additional tasks are not assessable for this lab. However you should find them useful for study purposes and to give you a deeper understanding of the material. PHP Variables Which of the following is a valid variable name and why? a) $my var1 b) $1myvar c) myvar1$ d) $my_var1 Which of the following sequences of commands will result in one being added to $a? a) $a++; b) $a = + 1; c) $b = $a + 1; d) all of the above CRICOS Provider No. 00103D ITECH3108 Lab 2 - Strings, Arrays and Loops.docx Page 3 of 5 What is the output of the following PHP code? Hello world What values will the variables $x, $y, and $z have after the following PHP lines? What values will the variables $y, $x, and $z have after the following PHP lines? Try to work it out manually. Looping and conditional statements  Show an "if" statement that checks if $name is equal to 'Harry';  Show an "if" statement that checks if $ii is more than 12.  Show a set of PHP statements that outputs 'hello' if $ii is equal to 0 and outputs 'goodbye' otherwise.  Show a set of PHP statements that outputs 'hello' if $name is equal to 'Harry', outputs 'goodbye' if $name is equal to 'Hardware' and outputs 'See You later' otherwise.  Why would you use a "switch" statement? What is its advantage? Arrays Answer the following questions (search the PHP website for answers). CRICOS Provider No. 00103D ITECH3108 Lab 2 - Strings, Arrays and Loops.docx Page 4 of 5 Where appropriate confirm your answers by writing and running your code. 1. How would you set the first item in an array named $fish to "tuna"? 2. Show a set of statements that outputs each element of an array called $mylist. 3. What are the array_push() and array_pop() functions used for? 4. What are the array_shift() and array_unshift() functions used for? 5. Assume the array $my_num contains all numeric values. Show a sequence of PHP statements that output the values of $my_num in ascending order? 6. What is the => operator used for? 7. How can you delete an element from an associative array? 8. How do the asort() and ksort() functions differ? 9. What is a multidimensional array? 10. What does the isset( ) function do? 11. Show a sequence of code that creates a sequential array containing the names of the months order. 12. Show a sequence of code that creates an associative array containing the names of the months in order as keys and number of days as value (e.g. 'Feb'=>28) 13. What are the max() and min() and array_sum() functions used for? 14. Why does PHP require braces ("{" and "}") when interpolating an element from a multidimensional array? CRICOS Provider No. 00103D ITECH3108 Lab 2 - Strings, Arrays and Loops.docx Page 5 of 5
To Download Click Here > ITECH3108 Dynamic Web Development Exercise.pdf
Codingassignmenthelper | Home Codingassignmenthelper | Home