You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/a-match-made-in-heaven/problem/problem.md
+3-5Lines changed: 3 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,19 @@
1
1
You have been given a piece of code (look for `a-match-made-in-heaven.php` in your working directory) which is full of bugs.
2
-
The code has been implemented badly, using a `switch` statement. Your job is to fix the code, by using the newly introduced
3
-
`match` expression in PHP 8.
2
+
The code has been implemented badly, using a `switch` statement. Your job is to fix the code, by using the newly introduced `match` expression in PHP 8.
4
3
5
4
The piece of code is supposed to take a string representing a keyboard keypress and convert it to its equivalent ANSI decimal code.
6
5
7
6
There are only four key presses supported at the minute (enter, up, down & escape). It should stay like that for now.
8
7
9
8
Focus on converting the switch statement to a match expression.
10
9
11
-
The key presses will be provided as strings via command line arguments. Only one keypress will be passed on each program invocation
12
-
but it will be randomly picked from the four supported key presses.
10
+
The key presses will be provided as strings via command line arguments. Only one keypress will be passed on each program invocation but it will be randomly picked from the four supported key presses.
13
11
14
12
### The advantages of match
15
13
16
14
* Match uses strict equality, unlike switch which uses weak comparison and can lead to subtle bugs.
17
15
* Each match arm does not fall through without a break statement, unlike switch.
18
-
* Match expressions must be exhaustive, if there is no default arm specified, and no arm matches the given value, an `UnhandledMatchError` is thrown
16
+
* Match expressions must be exhaustive, if there is no default arm specified, and no arm matches the given value, an `UnhandledMatchError` is thrown.
19
17
* Match is an expression and thus returns a value, reducing unnecessary variables and reducing the risk of accessing undefined variables.
If the `capital` property is null, the variable `$capitalPopulation` will also be null. Previously, without the null safe operator, this would be achieved like so:
13
+
If the `$capital` property is null, the variable `$capitalPopulation` will also be null. Previously, without the null safe operator, this would be achieved like so:
14
14
15
15
```php
16
16
$capitalPopulation = null;
@@ -44,25 +44,24 @@ Export the `$user` data to a CSV with the following columns:
* The columns should read exactly as above, any mistake will trigger a failure
49
-
* There should be one row for the column headers and one for the data
50
-
* Any properties which are null on the user should be printed as empty fields in the CSV
51
-
* The file should be named `users.csv` and exist next to your submission file (eg in the same directory)
47
+
* The CSV should be comma delimited.
48
+
* The columns should read exactly as above, any mistake will trigger a failure.
49
+
* There should be one row for the column headers and one for the data.
50
+
* Any properties which are null on the user should be printed as empty fields in the CSV.
51
+
* The file should be named `users.csv` and exist next to your submission file (eg in the same directory).
52
52
53
53
And finally, the most important part, all properties which may be `NULL` should be accessed using the null safe operator!
54
54
55
55
### Advantages of the null safe operator
56
56
57
-
* Much less code for simple operations where null is a valid value
57
+
* Much less code for simple operations where null is a valid value.
58
58
* If the operator is part of a chain anything to the right of the null will not be executed, the statements will be short-circuited.
59
-
* Can be used on methods where null coalescing cannot `$user->getCreatedAt()->format() ?? null` where `getCreatedAt()` could return null or a `\DateTime` instance
59
+
* Can be used on methods where null coalescing cannot `$user->getCreatedAt()->format() ?? null` where `getCreatedAt()` could return null or a `\DateTime` instance.
Remember your program will be passed no arguments. There will be a `User` object populated for you under the variable `$user`.
65
-
It is available at the beginning of your script.
64
+
Remember your program will be passed no arguments. There will be a `User` object populated for you under the variable `$user`. It is available at the beginning of your script.
66
65
67
66
Documentation on the Null Safe Operator can be found by pointing your browser here:
Create a program which contains one function named `logParameter`. It should have one parameter, and it's type must be `mixed`.
13
13
14
-
Within your function you should log the type of the parameter that was passed. For this, you can use a new PHP8 function called `get_debug_type`.
14
+
Within your function you should log the type of the parameter that was passed. For this, you can use a new PHP 8 function called `get_debug_type`.
15
15
16
16
This function will give you a string identifier representing the type of any PHP variable.
17
17
@@ -51,6 +51,6 @@ Documentation on `get_debug_type` can be found by pointing your browser here:
51
51
52
52
You might want to delete or empty your log file each time your program starts, otherwise it will grow and grow and the comparison will fail.
53
53
54
-
Think about the return type of your `adder` function - you could declare it as `void`.
54
+
Think about the return type of your `logParameter` function - you could declare it as `void`.
55
55
56
56
If you are curious how we compare the output of your program when it includes time, we simply check that it matches the format, rather than comparing exactly. More specifically, we use the regex `/\d{2}:\d{2}:\d{2}/`.
Copy file name to clipboardExpand all lines: exercises/caution-with-catches/problem/problem.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ function verify_password(string $password): bool;
11
11
12
12
For this exercise this function will always throw an exception but unfortunately the exception message contains the password in plain text!
13
13
14
-
To pass this exercise you will need to call the `verify_password` function with the password provided, handle the exception and output `"Given password is invalid, please try again`.
14
+
To pass this exercise you will need to call the `verify_password` function with the password provided, handle the exception and output `"Given password is invalid, please try again"`.
15
15
16
16
PHP 8 allows you to handle the exception without capturing the exception itself which will ensure this message is not leaked further.
Copy file name to clipboardExpand all lines: exercises/have-the-last-say/problem/problem.md
+13-19Lines changed: 13 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,33 +2,30 @@ Create a program that reads a CSV file using `fgetcsv` and change the delimiter
2
2
3
3
The first argument is the file name of the CSV which you should read.
4
4
5
-
The CSV file contains two columns separated with `|` (the pipe operator). The first column is `country`
6
-
and the second column is `capital`. You should print each row to the console like the
7
-
following (with a new line after each row):
5
+
The CSV file contains two columns separated with `|` (the pipe operator). The first column is `country` and the second column is `capital`.
6
+
7
+
You should print each row to the console like the following (with a new line after each row):
8
8
9
9
```
10
10
Country: Austria, Capital: Vienna
11
11
```
12
12
13
13
The list of countries will be picked at random, so the CSV will be different each time your program runs.
14
14
15
-
When using `fgetcsv` there's a bunch of arguments which change the behaviour of the way the CSV is parsed. In our case we
16
-
want to change the `separator` argument, which defaults to a comma `,`. We need it to be the
17
-
pipe `|` character.
15
+
When using `fgetcsv` there's a bunch of arguments which change the behaviour of the way the CSV is parsed. In our case we want to change the `separator` argument, which defaults to a comma `,`. We need it to be the pipe `|` character.
18
16
19
-
We don't want to specify the rest of the arguments, so aside from the file pointer which is the first argument,
20
-
we only want to specify the `separator` argument.
17
+
We don't want to specify the rest of the arguments, so aside from the file pointer which is the first argument, we only want to specify the `separator` argument.
21
18
22
19
Named arguments are a great way to change argument default values without having to specify all the defaults again.
20
+
23
21
For example, if you only want to change the value of the last argument to a function,
24
-
you can do so, without specifying all the other arguments. For example:
22
+
you can do so, without specifying all the other arguments:
We only want to change the last argument (double_encode) of the function to false (the default is true). However,
31
-
we are forced to specify all the other arguments, but they have not changed from the defaults.
28
+
We only want to change the last argument (double_encode) of the function to false (the default is true). However, we are forced to specify all the other arguments, even though they have not changed from the defaults.
32
29
33
30
Named arguments allows to write the same, but in a more succinct fashion:
34
31
@@ -40,8 +37,8 @@ Note: only the values changed from the defaults are specified!
40
37
41
38
### Advantages of named arguments
42
39
43
-
* Possible to skip defaults in between the arguments you want to change
44
-
* The code is better documented since the argument label is specified with the value, very useful for booleans
40
+
* Possible to skip defaults in between the arguments you want to change.
41
+
* The code is better documented since the argument label is specified with the value, very useful for booleans.
@@ -52,8 +49,7 @@ You will need to open the file for writing before using `fgetcsv` you can do tha
52
49
53
50
You will most likely need a loop to process all the data in the file.
54
51
55
-
You will need to keep reading from the file until it has been fully read. `feof` is your friend here to know
56
-
whether there is any data left to read.
52
+
You will need to keep reading from the file until it has been fully read. `feof` is your friend here and will inform you whether there is any data left to read.
57
53
58
54
Documentation on the `fopen` function can be found by pointing your browser here:
Although not entirely necessary for a small script, it is good practise to close
71
-
any open file handles so that other processes can access them, you can use `fclose`
72
-
for that.
66
+
Although not entirely necessary for a small script, it is good practise to close any open file handles so that other processes can access them, you can use `fclose` for that.
Copy file name to clipboardExpand all lines: exercises/lord-of-the-strings/problem/problem.md
+3-4Lines changed: 3 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,4 @@
1
-
PHP has a huge standard library with the most extravagant and highly specific string and array functions available.
2
-
`levenshtein` & `array_change_key_case` anyone??
1
+
PHP has a huge standard library with the most extravagant and highly specific string and array functions available. `levenshtein` & `array_change_key_case` anyone??
3
2
4
3
On the flip side, historically it has missed some very common string operations. There's always been a way to get the same results, but they are cumbersome and error prone.
5
4
@@ -19,7 +18,7 @@ Thankfully, PHP 8 has done away with all that nonsense. Please welcome the follo
0 commit comments