Week 5 Prac Exam

Updated on 28 Dec 2018

Instructions

  1. You are to submit ONE script which is to be named in the following format: (However you may find it beneficial to work with individual scripts for each question, providing that only ONE (final) script is submitted for assessment)
StudentID_comp306_prac1.php 
  1. You must clearly identify the code that belongs to each question, as shown with the PHP comments below.
//-----
//--Question 1
//-----
  1. Your php file must be emailed as an attachment to the following address ###@acu.edu.au by 7:00pm on the ##/##/####. Late submissions will be penalized!

  2. This is an open book exercise. You may use your textbook, lecture notes, notes taken during lectures and tutorials and the php.net website. Some questions may direct you to the PHP documentation website for further clarification.

  3. You may find it useful to examine the date, string, math and array functions on php.net when answering some of the questions.

  4. This exercise contributes 20% to your overall mark for this unit. Total marks available on this test is 20.

Question 1

2 marks

The following variables have been declared in a PHP script.

$name = 'Brent';
$mood = 'good';

Write a script so that the following message with the above variables would be displayed on the browser. Take note of the double quotes around good.

Hello, my name is Brent and I'm "good" today.

Question 2

4 marks

Complete the following snippet of code (at the point where it says to add your code here) so that the function returns the number of vowels in the variable that was passed to the function.

function get_number_of_vowels($string)
{
//…add your code here…
}

$name = 'Brent';
$num_vowels = get_number_of_vowels($name); 

echo "<p>$name has $num_vowels vowels in that word</p>";

Brent has 1 vowels in that word

Question 3

3 marks

Complete the following snippet of code (at the point where it says to add your code here) so that the contents of the variable are converted to lowercase except the last letter which will always be displayed in upper case.

$name = 'Brent';

//…add your code here…

echo $name;

Question 4

2 marks

The total cost including GST of an item is $19.95. To calculate the GST component, the total cost (including GST) is divided by 11.

$total_cost = 19.95;

Complete the script (using calculations in PHP) so that the browser has this output:

Base Cost: $18.14
GST Component: $1.81

Question 5

3 marks

From the previous question, add additional PHP code so that the base cost can be separated into dollar and cents amount as shown below.

Whole dollar amount: 18
Number of cents: 14

Question 6

2 marks

Use the PHP date function to display the current date and time in the browser similar to the format shown below.

31-August-2020  2:15pm

Question 7

4 marks

The following script contains a ‘mood’ array.

$moodArray = array('Great', 'Good', 'Happy', 'So-So', 'Upset');

Complete the script so that the following would be displayed to the browser, where # represents the number of elements in the array and # is determined by calling a php array function that returns the number of elements.

There are # moods available, and they are Great, Good, Happy, So-So, Upset