9e3f9013ed1e2f0236bfd253cace3df9
htmlphp
lab3
v2
rasb14
2017-09-12 07:09:25
v2.3.3 (2017-06-12)

Generated 2017-09-12 09:09:25 by dbwebb lab-utility v2.3.3 (2017-06-12).


Lab 3 - Htmlphp

If you need to peek at examples or just want to know more, take a look at the PHP Manual.

There you will find everything this lab will go through and much more.

1. Arrays - with numeric index

Exercise 1.1 (1 points)

Create an array, called countries with the items: [United Kingdom, Bahamas, Ukraine].

Answer with the second item in the array.

Exercise 1.2 (1 points)

Create a new array called capitals with the items: [London, Nassau, Kiev].

Answer with a string containing each country from the countries-array followed by the corresponding capital. Use the format "country = capital, country = capital..."

Exercise 1.3 (1 points)

Create an array, called numbers1 with the items: [522, 87, 54.98, 3, 6533]. Answer with the sum of the first two items.

Exercise 1.4 (1 points)

Use your arrays numbers1 and capitals to change item at index 4 in numbers1 to the item at index 2 in capitals. Answer with the array numbers1.

Exercise 1.5 (1 points)

Use your array countries and concatenate the first and the last item as a string. Separate the items with a hyphen - and answer with the result.

2. Arrays - with keys

Exercise 2.1 (1 points)

Use your countries and capitals arrays and create another array called keyArray. The country should be the key and the capital should be the value. Answer with the new array. (hint: "country" => "capital")

Exercise 2.2 (1 points)

Add the key "Canada" with the value "Ottawa" to your keyArray. Answer with the updated array.

Exercise 2.3 (1 points)

Answer with the value for the key "Bahamas".

3. Arrays - built-in functions

Exercise 3.1 (1 points)

Find the number of items in the array [324, 36, 20.02, 8, 4998]. Answer with the result as an integer.

Exercise 3.2 (1 points)

Sort the array [324, 36, 20.02, 8, 4998] in decending order. Make sure that it does maintain the key association. Answer with the sorted array.

Exercise 3.3 (1 points)

Use pop() on the array [324, 36, 20.02, 8, 4998] and answer with the popped item.

Exercise 3.4 (1 points)

Use push() on the array [285, 11, 9.75, 9, 2216] and insert the number 56. Answer with the resulting array.

Exercise 3.5 (1 points)

Copy your array countries to a new array called newArray. Use shift() on the new array and answer with the shifted item.

Exercise 3.6 (1 points)

Use unshift() on your newArray add the items "Montana" and "Oregon" in the beginning of the array. Answer with the modified array.

Exercise 3.7 (1 points)

Reverse the order of the array [324, 36, 20.02, 8, 4998]. Answer with the modified array.

Exercise 3.8 (1 points)

Use implode() on your capital-array and answer with a string where each item is separated by a hyphen -.

4. Arrays - for-each loop

Exercise 4.1 (1 points)

Create an array, called numbers1 with the items: [21, 5, 4, 6, 76, 2, 18, 85, 55, 1]. Use a for-each loop to sum each item with 6 and put them in a new array called summedNumbers1. Answer with the new array.

Exercise 4.2 (1 points)

Create a new array called numbers2 with the items [11, 4, 41, 67, 99, 22, 8, 83, 5, 16]. Use at least a for-each loop to add all numbers larger than 20 to a new array called largeNumbers. Answer with the new array.

Exercise 4.3 (1 points)

Create an array with the keys: "one", "two", "three", "four" and "five" and the values: 1, 2, 3, 4, 5. Use a foreach-loop to add all keys and values to an array in the format: ["key"=value, "key"=value, etc]. Use implode() to make the answer a string with all items separated by a comma ,.

5. Switch-case

PHP Manual about switch

Exercise 5.1 (1 points)

Create a switch-case statement that returns the continent a certain country resides in. Use the countries: "Sweden, Brazil, China, Australia, Canada" and the continents:
"Europe, South America, Asia, Oceania, North America".

Answer with a test on the country: "Canada".

6. Extra assignments

These questions are worth 3 points each. Solve them for extra points.

Exercise 6.1 (3 points)

A switch-case statement can handle multiple cases with the same result. Create a new switch-case statement that returns the corresponding continent. Use the countries: "Sweden, Denmark, Finland, Brazil, China, Australia, Canada" and the continents: "Europe, South America, Asia, Oceania, North America".

Answer with a test on the country: "Denmark".

Exercise 6.2 (3 points)

Sort the array [285, 11, 9.75, 9, 2216] in ascending order. Make sure that it does not maintain the key association. Answer with the sorted array.


9e3f9013ed1e2f0236bfd253cace3df9