Get up to 80 % extra points for free! More info:

Discussion – Solved tasks for PHP lesson 1-4

Back

 

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
3/24/2017 18:26
Avatar
Justin Jake Telo:2/4/2018 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
2/4/2018 10:38
Avatar
Mario
Member
Avatar
Mario:2/8/2018 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 2/8/2018 2:19
 
Reply
2/8/2018 2:18
Avatar
oxana
Member
Avatar
oxana:6/1/2019 3:35
<html>
  <head>
    <title>PHP Test</title>
  </head>
  <body>
    <?php

     /*

    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:

    */

    $a = 10;
    $b = 20;

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

    echo "Rectangle with length of sides $a cm and $b cm
    has an area of $area cm <sup>2</sup> and perimeter of $perimeter cm <br />";


    /*

    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.

    */


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


    echo "Third product is deviated from an avarege price by $deviation <br />";


    /*



    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.


    */



    $zodiac_animals = array();

    $zodiac_animals[2016] = 'monkeys';
    $zodiac_animals[2017] = 'roosters';
    $zodiac_animals[2018] = 'dogs';
    $zodiac_animals[2019] = 'swine';
    $zodiac_animals[2020] = 'rats';
    $zodiac_animals[2021] = 'buffalos';
    $zodiac_animals[2022] = 'tigers';
    $zodiac_animals[2023] = 'rabbits';
    $zodiac_animals[2024] = 'dragons';
    $zodiac_animals[2025] = 'snakes';
    $zodiac_animals[2026] = 'horses';
    $zodiac_animals[2027] = 'sheep';

    $year = 2019;

    echo "It' year of $zodiac_animals[$year] <br />";




    $zodiac_animals2 = array (
      'monkeys','roosters','dogs','swine','rats','buffalos','tigers','rabbits','dragons','snakes','horses','sheep');

    $year2 = 2022;


    $answ_year = $year2 % 12;


    echo "It' year of $zodiac_animals2[$answ_year] <br />";

    ?>
  </body>
</html>
Edited 6/1/2019 3:36
 
Reply
6/1/2019 3:35
Avatar
Replies to oxana
David Capka Hartinger:6/1/2019 5:37

The second version of your zodiac animals is better since the array declaration is much more compact :)

Reply
6/1/2019 5:37
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
oxana
Member
Avatar
 
Reply
6/1/2019 22:37
Avatar
Alladin Solomon:7/4/2020 11:48
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <!-- User input the year here -->
        <form action="index.php" method="post">
            Inter you birth Year: <input type="number" name ="year">
            <input type="submit">
        </form>
        <?php
            // Set Zodiac Array here
            $zodiac = array("rats","buffallo","tigers","rabbits","dragons","snakes","horses","goats","sheep","monkeys","roosters","dogs","swine");

            // Set the Year here per zodiac same array count
            $year = array(2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020);

            // Get the user year input
            $yearInput = filter_input(INPUT_POST, 'year');

            // count the arrays in $year variable
            $count = count($year);

            // Set boolean to stop the loop if already found the zodiac year
            $found = false;

            // Search for same year the user input in the current $year array values
            look:
            for ($i=0; $i<$count; $i++){
                if ($yearInput == $year[$i]){
                    echo "$yearInput is year of the $zodiac[$i]";
                    $found = true;
                }
            }
            // If user input year not found in the current $year array.
            // Set the current array with new year then look again
            if (!$found){
                if ($yearInput < $year[0]){
                    for ($i=0; $i <$count; $i++){
                        if ($i==0){
                            $year[$i] -= ($count - ($i));
                        } else {
                            $year[$i] = $year[$i-1] + 1;
                        }
                    }
                } elseif ($yearInput > $year[$count-1]){
                    for ($i=0; $i <$count; $i++){
                        if ($i==0){
                            $year[$i] = ($year[$count-1] + 1);
                        } else {
                            $year[$i] = $year[$i-1] + 1;
                        }
                    }
                }
                goto look;
            }
        ?>
    </body>
</html>
 
Reply
7/4/2020 11:48
Avatar
Aleksa Aleksic:6/14/2021 18:09
<?php
$zodiac = array(Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig);
$currentYear = date('Y');
$currentAnimal = $zodiac [$year - $currentYear +1];

echo "It`s year of " . $currentAnimal ."<br /> <strong>Are you born in the year of $currentAnimal ?</strong>";
?>
 
Reply
6/14/2021 18:09
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

8 messages from 8 displayed.