Exercises for PHP lesson 1-4

PHP Basic constructs Exercises 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:

Rectangle
localhost

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:

Products
localhost

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:

Zodiac
localhost

 

Download

Downloaded 97x (2.47 kB)
Application includes source codes in language PHP

 

 

Article has been written for you by David Capka
Avatar
Do you like this article?
3 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn College The author learned IT at the Unicorn College - a prestigious college providing education on IT and economics.
Thumbnail
Previous article
Strings and arrays in PHP
Thumbnail
All articles in this section
PHP basic constructs
Activities (8)

 

 

Comments

Avatar
David McQarphui:3/24/2017 18:26
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
    <head>
        <title>Test</title>
    </head>
    <body>

        <?php

            $a = 12;
            $b = 24;

            $perimeter = 0;
            $area = 0;

            $perimeter += (2 * $a) + (2 * $b);
            $area += $a * $b;

            echo "Rectangle with length of sides " . $a . " cm and " . $b .
            " cm has an area of " . $area . " and perimeter of " . $perimeter . " cm.";

        ?>
        <br/><hr/>

        <?php

            $average_price = 0;
            $deviation = 0;

            $product_prices = array(350, 280, 128, 192, 432);
            $average_price += array_sum($product_prices) / count($product_prices);
            $deviation += $average_price - $product_prices[2];

            echo "Third product deviated from an average price by $" . $deviation . ".";

        ?>
        <br/><hr/>

        <?php

            $zodiac_animals = ['Rat', 'Buffalo', 'Tiger', 'Rabbit', 'Dragon', 'Snake',
                               'Horse', 'Goat', 'Sheep', 'Monkey', 'Rooster', 'Dog', 'Swine'];
            $year = 2016;

            $current_animal = (($year - 4) % 13);

            echo "It's the year of " . $zodiac_animals[$current_animal];

        ?>

    </body>
</html>
 
Reply  +1 3/24/2017 18:26
Avatar
Justin Jake Telo:4. February 10:38

<?php

//variable for test 3
$zodiac = ["rats", "buffalos", "tigers", "rabbit", "dragons", "snakes", "horse", "goat",
"sheep", "monkeys", "rooster", "dogs", "swine"];
$year = date('Y');

switch($year){
case 2010:
echo "it year of rats";
break;

case 2011:
echo "it year of Buffalos";
break;

case 2012:
echo "it year of tiger";
break;

case 2013:
echo "it year of rabbit";
break;

case 2014:
echo "it year of dragons";
break;

case 2015:
echo "it' year of snakes";
break;

case 2016:
echo "it' year of horse";
break;

case 2017:
echo "it' year of goat";
break;

case 2018:
echo 'it\'s year of sheep';
break;

case 2019:
echo "it\' year of monkeys";
break;

case 2020:
echo "it\' year of rooster";
break;

default:
echo "your zodiac sign is not yet coming";
}
?>

 
Reply 4. February 10:38
Avatar
Mario
Member
Avatar
Mario:8. February 2:18
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Exercises for PHP lessons 1-4</title>
    </head>
    <body>
                <h1><strong>Exercises for PHP lessons 1-4</strong></h1>
                <h2>Easy exercise</h2>
                <p>Area and perimeter of 7cm * 12cm rectangle?</p>
        <?php
            $a = 7;
            $b = 12;
            $perimeter = (2 * $a) + (2 * $b);
            $area = $a * $b;
        ?>
                <p>
                        <ul>
                                <li>Rectangle with sides length of <?php echo $a; ?> cm and <?php echo $b; ?> cm has <strong>area</strong> of <strong><?php echo $area; ?> cm²</strong> and <strong>perimeter</strong> of <strong><?php echo $perimeter; ?> cm</strong></li>
                        </ul>
                </p>
                <hr/>
                <h2>Intermediate exercise</h2>
                <p>3rd product price deviation from average product prices (1st $350; 2nd $280; 3rd $128; 4th $192; 5th $432)</p>
        <?php
            $prices = array(350, 280, 128, 192, 432);
            $average_price = array_sum($prices) / count($prices);
            $deviation = $average_price - $prices[2];
        ?>
                <p>
                        <ul>
                                <li>Third product deviated from an average price by: <strong>$<?php echo $deviation; ?></strong>.</li>
                        </ul>
                </p>
                <hr/>
                <h2>Advanced exercise</h2>
                <p>Year on exercise day: 2018.</p>
        <?php
                        $year = 2018;
            $animals = ['Rat', 'Buffalo', 'Tiger', 'Rabbit', 'Dragon', 'Snake', 'Horse', 'Goat', 'Sheep', 'Monkey', 'Rooster', 'Dog', 'Swine'];
            $year_animal = ($year - 4) % 12;
        ?>
                <p>
                        <ul>
                                <li>Year <?php echo $year; ?>. is the year of <strong><?php echo $animals[$year_animal]; ?></strong>.</li>
                        </ul>
                </p>
                <hr/>
    </body>
</html>
Edited 8. February 2:19
 
Reply  +1 8. February 2:18
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

3 messages from 3 displayed.