DT PHPBasics Selection
DT PHPBasics Selection
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
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).
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 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
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.
$number_of_adults = 4;
$number_of_children = 29;
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:
echo <<<MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5. Remainder\n
MENU;
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.