0% found this document useful (0 votes)
4 views3 pages

DT PHPBasics Selection

The document outlines a series of programming tasks to be completed in PHP, organized into five distinct tasks. Each task requires the implementation of specific functionality, such as determining odd/even numbers, generating messages based on random numbers, calculating ticket costs with discounts, and performing arithmetic operations based on user input. The tasks are to be saved as individual PHP files within a designated folder named 'Selection'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

DT PHPBasics Selection

The document outlines a series of programming tasks to be completed in PHP, organized into five distinct tasks. Each task requires the implementation of specific functionality, such as determining odd/even numbers, generating messages based on random numbers, calculating ticket costs with discounts, and performing arithmetic operations based on user input. The tasks are to be saved as individual PHP files within a designated folder named 'Selection'.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Task Sheet

Selection
The following tasks should be run from a command prompt.

Each task should be placed in a separate file within a folder called “Selection”. Call your files
“Task1.php”, “Task2.php”, etc.

Task 1
You have been given the following code to type at the start of “Task1.php”:
<?php

$number = rand(1, 20);

Write some code that outputs whether the random $number variable contains an odd or an
even number (include the value of $number in your output to verify).

You must use an if statement for this task.

Task 2
Using either if-elseif-else or switch complete the following code so that a different message
is output depending on the value of the random number that is generated. Do not change
the range of the random numbers that are generated – your code should handle numbers
that fall outside of your required range.
<?php

$number = rand(0, 8);

Number Message
1 One for sorrow
2 Two for joy
3 Three for a girl
4 Four for a boy
5 Five for silver
6 Six for gold
7 Seven for a secret never to be told
Any other number Not a permitted number
Task 3
Using a switch statement, complete the following code so that the playing card, that
corresponds to the randomly generated number, is output.
<?php

$number = rand(1, 13);

Number
Value displayed
generated
1 Ace
2 – 10 Face value, so if a 2 display a 2 etc.
11 Jack
12 Queen
13 King

Task 4
Write a program that works out the total purchase cost of tickets to the theatre and outputs
that cost to the screen.

The prices are as follows:

• An adult ticket costs £10.50


• A child ticket costs £7.30

The discounts are as follows:

• 1 free adult ticket for every 10 child tickets sold


• 10% discount if the total bill exceeds £100 after any free tickets have been dealt
with

Use the following code to start you off.


<?php

$number_of_adults = 4;
$number_of_children = 29;

Don’t alter the number of adults or the number of children.

Task 5
The following code prompts the user to enter two integers (either may be positive or
negative). It then displays the following menu so that the user can select the operation they
want:

Philip Windridge <[email protected]>


2
1. Add
2. Subtract
3. Multiply
4. Divide
5. Remainder
<?php

$int1 = readline("Enter the first integer: ");


$int2 = readline("Enter the second integer: ");

echo <<<MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5. Remainder\n
MENU;

$operation = readline("Select the menu number: ");

Your task is to add to the above code so that the correct operation is carried out on the two
numbers that the user has entered.

Each arithmetic operation must be implemented using a function.

The generic format for each operation is as follows:

Purpose Perform indicated operation on the two integers and output


the result (for example, if the user has selected 1 it adds the
two integers)
Identifier (function name) Appropriate to the operation
Parameters The two operands
Return Type void

Philip Windridge <[email protected]>


3

You might also like