T_PHPBasics_Iteration
T_PHPBasics_Iteration
Iteration
Task 1
You are to create a program that prints a grid of asterisks.
The following code prompts a user to enter the number of columns they would like and the
number of rows they would like.
<?php
You must add to the code so that the grid is output in the correct number of rows and
columns. For example, two rows and three columns should produce the following output.
* * *
* * *
Task 2
You are to create a program that simulates throwing two dice and counts the number of
throws that are needed until the dice match.
When the dice match your program should output the number of throws and the final
matching values.
Dice 1: 2 | Dice 2: 6
Dice 1: 5 | Dice 2: 3
Dice 1: 5 | Dice 2: 5
Matching pair in 3 turns!
Task 3
Write a game of Paper, Rock, Scissors; where the user is prompted to enter their choice as a
case-insensitive string (e.g. Paper or paper would be valid inputs) and the computer's choice
is generated randomly.
The game should continue playing until either the user or the computer scores 3 winning
games.
At the conclusion of the game your program should output the result (who won) and the
scores of both players.
Those not familiar with the game can learn a little about it here
https://en.wikipedia.org/wiki/Rock-paper-scissors. Just as a reminder:
Task 4
Write a program that stores the colours of the rainbow in order in an array. The program
will then continually prompt the user to enter an integer from 1 to 7 or -1 to end the
program. When the user enters an integer between 1 and 7 it will output the corresponding
colour from the rainbow.
For example, if the user enters 3 your program should output Yellow.
Write and test your program ensuring you test all 7 values and the exit value.
If you are unsure about the colours of the rainbow you can follow this link:
https://www.metoffice.gov.uk/learning/rainbows/colours-of-the-rainbow.
Task 5
The following code contains an array that holds the titles of five films.
<?php
$films = array(
'The Matrix', 'Man of Steel', 'Iron Man',
Your task is to figure out how to sort the array so that it is in alphabetical order and then
output it as a list.
Task 6
Add to the following code so that the two-dimensional array $one_to_five_times_table is
output with each ‘table’ on separate lines.
<?php
$one_to_five_times_table = [
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15],
[4, 8, 12, 16, 20],
[5, 10, 15, 20, 25]
];