Web applications

Tasks studies - laboratory


Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko

Web Applications 1 (AI1) – Lab 2

Basics of PHP 8

Start of the lab:

• download the Lab002_AI1_start.zip archive to your desktop, which contains the files needed to complete the tasks, and unpack this archive,

• go to the unpacked folder and open the lab2 folder in VSCode,


lab2

• open the terminal, select CMD (not PowerShell),


lab2

• execute the script from the file abc.php, the expected result is to display Hello,


lab2

• solve each of the following tasks in zadX.php files, where X is the task number (2.X) and run the scripts according to the above method,

• use the PHP documentation: link or the book mentioned in “AI1 Issues from the PHP book.pdf”,

• tasks/fragments of tasks marked * should be omitted, they will be for independent completion in the event of insufficient time for the lab,

Tasks (PHP 8):

Task 2.1:

Using one print statement, display the following text on the console:

‘Hello world!’ means “Hello world!”.

This is the text that appears in the first programs.

Task 2.2:

Create:

• variable a and assign it the value 4,

• constant B with the value 10,

• variable c with 4.0,

• d with 5.667.

Print a sentence to the console with information about the operation being performed and/or its result:

• adding a and B,

• dividing a by B *,

• a to the power of B *,

• remainder of dividing B by a *,

• whether a has the same value as B *,

• whether the value of a is greater than B,

• whether the value of a is greater than B (using the ternary operator) *,

• whether a and c have the same value,

• whether a and c have the same value (taking into account the type),

• d without a decimal place,

• d rounded to 2 decimal places *.

The sum of … and … is …
…
They are different./They are equal.
It is greater./It is less./They are equal.
define, PHP_EOL, round

Task 2.3: *

Create a and B with values ​​as in the previous task.

Try to assign new values ​​to them: 7 to a and 22 to B. Explain the situation in the comment.

Task 2.4:

There are two variables storing strings, containing excess spaces:

$text1 = "I program well"; $text2 = "well in PHP. ";

Then display on the console:

• length of variable text1,

• variable text2 in reverse order (backwards) *,

• which variable contains the longer string of characters? *,

• does variable text1 contain the word Programming?,

• does variable text2 start with the word well? *,

• combined variables text1 and text2 with previously removed excess spaces,

• place the result from the above pause in variable text3, divide by the separator being a space, and display the resulting array *,

• variable text1 with the word well changed to bad *,

• at which index (position) does the word PHP start in text2?,

• variable text1 with all letters changed to uppercase,

• variable text2 with the first letter changed to uppercase *,

• variable text2 in the range from 9 to 11 positions inclusive *.

mb_strlen, strrev, str_contains, str_starts_with, trim, explode, str_replace,
mb_strpos, mb_strtoupper, ucfirst, mb_substr

Task 2.5: *

There is a variable n (it has a value of 3.5) and a variable note (it has no value).

Use a conditional switch construction, which, based on the assumed value (in the range of 2.0, …, 5.0), assigns the word name of the grade to the variable note.

If a value is given outside the range, assign an empty string to the variable note.

Run this task again using the match construction.

inadequate
...
very good

Task 2.6:

Create your own function called ctf, whose task will be to convert degrees Celsius to Fahrenheit, accepting one optional float parameter called c, with a default value of null.

In the case of calling the function:

• without a parameter, the message “No value provided” should be displayed and null should be returned,

• if a parameter value is provided, the converted value in Fahrenheit should be returned (also float).

°F = (°C × 9/5) + 32

Task 2.7: *

The variable l has the value 10. Create your own function rd, which will change the value of this variable to a random number from the range 1-50.

random_int

Task 2.8: *

Print out numbers from 0 to 100 in steps of 5 (0, 5, 10, …, 95, 100), excluding those that are divisible by 7.

Task 2.9:

Create an array named fruits with numeric keys containing the names of fruits listed below. Perform the following pauses:

• display the number of elements in the list,

• display all elements, each on a separate line,

• add a lemon to the end of the list,

• remove the last element of the list,

• display an array sorted in descending order *.

"banana", "apple", "strawberry", "grape", "orange", "watermelon", "blueberry"
count, array_push, print_r, array_pop, sort

Task 2.10:

Create an array called people with keys that are strings of characters, being the names of people, and the values ​​will be the current age of the person. Create an array with sample people. Then:

• display all elements, each on a separate line, in the form as in the following,

• display the number of elements in the list,

• display the age of Mr. Bartosz,

• add Mr. Witold who is 28 years old,

• remove Mr. Piotr,

• display the array sorted descending by the age of the people.

Jan is 45 years old, Bartosz is 38 years old, Piotr is 40 years old
count, unset, arsort, print_r

Task 2.11: *

Create your own function called division, whose task will be to return the result of dividing two integers (taken as parameters x, y). Test the function for several sets of different parameters. Then:

• inside the function, report a problem when the second parameter is 0 (this is an invalid value for the further operation of the function, leading to division by 0),

• at the place of the function call, handle the situation if the second parameter is 0,

• inside the function, report a problem when any of the parameters has a type other than int (this is an invalid value for the further operation of the function, the function is only supposed to perform integer division),

• at the place of the function call, handle the situation if any of the parameters is a value other than an integer. ``` InvalidArgumentException, TypeError, getMessage


## [Task 2.12](https://github.com/dawidolko/Web-Applications/blob/main/LAB02/task/zad12.php): *
Display:

• current date in the format: Thursday, 04-03-2024,

• current date and time in the format: 2024-March-04 10:10,

• number of days between today and March 12, 2021,

• number of hours and minutes between the current time and 7:00 today,

• which date is earlier: today's date or April 1, 2023.

date, mktime, strtotime, new DateTime(…), getTimestamp, date_diff, setTime, format


## [Task 2.13](https://github.com/dawidolko/Web-Applications/blob/main/LAB02/task/zad13.php):
Implement the Point class that allows you to represent points in a 2D
plane (x and y coordinates). The class should provide the following functionality:
• the ability to print information about a point in the form of Point(…,…), where … are the values
of coordinates,

• the ability to update a given coordinate for a point,

• moving a point by a given distance.

Then create several sample points and test the operation of all
components of the class.

class, **construct, set…, get…, **toString, new …(…)


## [Task 2.14](https://github.com/dawidolko/Web-Applications/blob/main/LAB02/task/zad14.php): *
Implement the Dog class representing dogs from the shelter, each of them has
a universal identifier (v4), name, age, date of adoption. The class should provide
the functionality of printing information about the object (in the form presented below).

To create random identifiers, install the ramsey/uuid package with Composer
(execute the command below).

Then create 5 sample dogs, add them to the list and test their
display.

`Note: VSCode may underline some lines incorrectly.`
```php
composer require ramsey/uuid
Burek (9 years old) accepted on 10-03-2024
Clifford (9 years old) accepted on 05-02-2024
Azor (12 years old) accepted on 15-02-2024
Szarik (8 years old) accepted on 22-02-2024
Idefix (15 years old) accepted on 26-01-2024
Uuid::uuid4()

After completing the lab, delete all downloaded and self-created files from the computer in the lab room.

File version: v1.0