Solved tasks for PHP lesson 1-4
The following exercises will help you put your knowledge of PHP from previous lessons to the test. Try to solve them on your own, but know that you could always download the working solutions under the article. Nonetheless, beware that when you view the solution without solving the exercise, you won't learn anything
If you find yourself stuck at some point, you should always try going back to check previous tutorials first and figure the problem out by yourself.
Easy exercise
Create a script that declares the variables $a
and
$b
. Store lengths of the sides of a rectangle in these variables
(you get to pick the values) and then print the perimeter and the area of the
rectangle. Output the data in the following format using string
concatenation:
Application screen sample:
Intermediate exercise
Write a script that stores products with the following prices: $350, $280, $128, $192, $432. Calculate the deviation of the 3rd product from the average price. Use an array to solve this task.
Application screen sample:
Advanced exercise
In Chinese astrology, each year has a zodiac animal assigned to it. The
zodiac animals are rats
, buffalos
,
tigers
, rabbits
,
dragons
, snakes
,
horses
, goats
,
sheep
, monkeys
,
roosters
, dogs
and
swine
. Create a script that prints the zodiac animal for a
year stored in a $year
variable. The year 2017 is the year of the
monkey, 2018 is the year of the rooster, and so on. Signs loops in the given
order forever, however, since you don't know how to write conditions yet, making
it work for the next 12 years will suffice.
Application screen sample:
Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.
Download
By downloading the following file, you agree to the license terms
Downloaded 148x (2.47 kB)
Application includes source codes in language PHP
In order to absolve this article, please pass all exercises above by submitting them.