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/baby-steps/problem/problem.md
+16-3Lines changed: 16 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,16 +8,24 @@ You can access command-line arguments via the global `$argv` array.
8
8
To get started, write a program that simply contains:
9
9
10
10
```php
11
+
<?php
11
12
var_dump($argv);
12
13
```
13
14
15
+
{{ cli }}
14
16
Run it with `php program.php` and some numbers as arguments. e.g:
15
17
16
18
```sh
17
19
$ php program.php 1 2 3
18
20
```
19
21
20
-
In which case the output would be an array looking something like:
22
+
{{ cli }}
23
+
24
+
{{ cloud }}
25
+
Run it by pressing the `Run` button in the editor.
26
+
{{ cloud }}
27
+
28
+
The output should be an array looking something like:
21
29
22
30
```php
23
31
array(4) {
@@ -32,10 +40,15 @@ array(4) {
32
40
}
33
41
```
34
42
35
-
You'll need to think about how to loop through the number of arguments so you can output just their sum. The first element of the `$argv` array is always the name of your script. eg `program.php`, so you need to start at the 2nd element (index 1), adding each item to the total until you reach the end of the array.
43
+
You'll need to think about how to loop through the number of arguments so you can output just their sum. The first element of the `$argv` array is always the name of your script. eg `solution.php`, so you need to start at the 2nd element (index 1), adding each item to the total until you reach the end of the array.
36
44
37
45
Also be aware that all elements of `$argv` are strings and you may need to *coerce* them into numbers. You can do this by prefixing the property with a cast `(int)` or just adding them. PHP will coerce it for you.
38
46
39
-
`{appname}` will be supplying arguments to your program when you run `{appname} verify program.php` so you don't need to supply them yourself. To test your program without verifying it, you can invoke it with `{appname} run program.php`. When you use `run`, you are invoking the test environment that `{appname}` sets up for each exercise.
47
+
{{ cli }}
48
+
{{ appname }} will be supplying arguments to your program when you run `{{ appname }} verify program.php` so you don't need to supply them yourself. To test your program without verifying it, you can invoke it with `{{ appname }} run program.php`. When you use `run`, you are invoking the test environment that {{ appname }} sets up for each exercise.
49
+
{{ cli }}
40
50
51
+
{{ cloud }}
52
+
PHP school will be supplying arguments to your program when you click `Verify` in the editor. To test your program without verifying it, you can just execute it by pressing the `Run` button in the editor.
Copy file name to clipboardExpand all lines: exercises/concerned-about-separation/problem/problem.md
+6-11Lines changed: 6 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
-
This problem is the same as the previous but introduces the concept of **classes**. You will need to create two files to solve this.
1
+
This problem is the same as the previous but introduces the concept of **classes**. You will need to create two files to solve this problem.
2
2
3
3
Create a program that prints a list of files in a given directory, filtered by the extension of the files. The first argument is the directory name and the second argument is the extension filter. Print the list of files (one file per line) to the console.
4
4
5
-
You must write a *class* file to do most of the work. The file must *define* a single class with a single function that takes **two** arguments: the directory name and the filename extension string in that order. The filename extension argument must be the same as what was passed to your program. Don't turn it into a regular expression or prefix with "." or do anything except pass it to your class method where you can do what you need to make your filter work.
5
+
You must write a *class*in a file to do most of the work. The file must *define* a single class with a single function that takes **two** arguments: the directory name and the filename extension, in that order. The filename extension argument must be the same as what was passed to your program. Don't turn it into a regular expression or prefix with `.` or do anything except pass it to your class method where you can do what you need to make your filter work.
6
6
7
7
You **must** not print directly to the console from your class, only from your original program.
8
8
9
-
The benefit of having a contract like this is that your class can be used by anyone who expects this contract. So your class could be used by anyone else who does learnyouphp, or the verifier, and just work.
9
+
The benefit of having a contract like this is that your class can be used by anyone who expects this contract. So your class could be used by anyone else who attempts this exercise, or the verifier, and just work.
To use your new class in your original program file, use the `require_once` construct with the filename. So, if your file is named mymodule.php then:
28
+
To use your new class in your original program file, use the `require_once` construct with the filename. So, if your file is named `mymodule.php` then use:
29
29
30
30
```php
31
31
<?php
@@ -41,12 +41,7 @@ $myFilter = new DirectoryFilter;
41
41
42
42
You can then call the method you defined with its required arguments.
Copy file name to clipboardExpand all lines: exercises/database-read/problem/problem.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,22 +4,22 @@ Display the information of all the users in the database table `users` whose age
4
4
5
5
`User: Jim Morrison Age: 27 Sex: male`
6
6
7
-
Finally you will be given a random name as the second argument to your program, you should update the row in the `users` table which corresponds to this name. You should change the name to `David Attenborough`.
7
+
Finally, you will be given a random name as the second argument to your program, you should update the row in the `users` table which corresponds to this name. You should change the name to `David Attenborough`.
This is an exercise introducing databases and PDO. PDO is a powerful abstraction library for dealing with different database vendors in a consistent manner. You can read the PDO manual here:
The most interesting class will be `\PDO`. The first parameter is the DSN string. The second and third are the username and password for the database. They are not needed for this exercise and can be left out.
21
21
22
-
The `users` table is structured as follows
22
+
The `users` table is structured as follows:
23
23
24
24
```
25
25
+----+-----------------+-----+--------+
@@ -40,11 +40,11 @@ foreach ($pdo->query('SELECT * FROM users') as $row) {
40
40
}
41
41
```
42
42
43
-
`$row` is now an array of data. The key will be the columns and the value is the database value
43
+
`$row` is now an array of data. The key will be the column name and the value is the database value.
44
44
45
45
46
46
You should use prepared statements to perform the updating. You should be most interested in the `prepare` and `execute` methods.
47
47
48
-
Remember the first argument will be the program's file path and not an argument passed to the program.
48
+
Remember, the first argument will be the program's file path and not an argument passed to the program.
Copy file name to clipboardExpand all lines: exercises/dependency-heaven/problem/problem.md
+9-3Lines changed: 9 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,8 +38,8 @@ The library works by accepting a PSR7 request and returns to you a PSR7 response
38
38
39
39
There are a few other components we need, in order to use `league/route`:
40
40
41
-
***laminas/laminas-diactoros** - For the PSR7 requests and responses.
42
-
***laminas/laminas-httphandlerrunner** - For outputting the PSR7 response to the browser.
41
+
*`laminas/laminas-diactoros` - For the PSR7 requests and responses.
42
+
*`laminas/laminas-httphandlerrunner` - For outputting the PSR7 response to the browser.
43
43
44
44
`laminas/laminas-diactoros` is a PSR7 implementation. PSR's are standards defined by the PHP-FIG, a committee of PHP projects, attempting to increase interoperability in the PHP ecosystem. PSR7 is a standard for modelling HTTP requests. We can use the `laminas/laminas-diactoros` package to marshal a PSR7 request object from the PHP super globals like so:
45
45
@@ -91,9 +91,15 @@ Finally, you will also be required to use `symfony/string` to manipulate the dat
Point your browser to [https://getcomposer.org/doc/00-intro.md](https://getcomposer.org/doc/00-intro.md) which will walk you through **Installing Composer** if you haven't already!
95
96
96
-
Use `composer init` to create your `composer.json` file with interactive search.
97
+
Use `composer init` to create your `composer.json` file with interactive search.
98
+
{{ cli }}
99
+
100
+
{{ cloud }}
101
+
Composer is installed and ready to go on cloud, use the `Composer Deps` button in the editor to search for and install your dependencies. While you should read the documentation for [Composer](https://getcomposer.org/doc/00-intro.md), it's important to note that the way we manage dependencies on PHP School cloud, is not how you would manage them in your own projects. We abstract away the `composer.json` file to keep it simple.
Create a program that prints a list of files in a given directory, filtered by the extension of the files. You will be provided a directory name as the first argument to your program (e.g. '/path/to/dir/') and a file extension to filter by as the second argument.
1
+
Create a program that prints a list of files in a given directory, filtered by the extension of the files. You will be provided a directory name as the first argument to your program (e.g. `/path/to/dir/`) and a file extension to filter by as the second argument.
2
2
3
-
For example, if you get 'txt' as the second argument then you will need to filter the list to only files that **end with .txt**. Note that the second argument _will not_ come prefixed with a '.'.
3
+
For example, if you get `txt` as the second argument then you will need to filter the list to only files that **end with .txt**. Note that the second argument _will not_ come prefixed with a full stop (`.`).
4
4
5
-
The list of files should be printed to the console, one file per line.
5
+
The list of files should be printed out, one file per line.
The `DirectoryIterator` class takes a pathname as its first argument. Using an iterator in a `foreach` loop will provide you with a `SplFileInfo` object for each file.
10
+
The `DirectoryIterator` class takes a pathname as its first argument.
11
+
12
+
Using an iterator in a `foreach` loop will provide you with a `SplFileInfo` object for each file.
11
13
12
14
```php
13
15
<?php
@@ -16,13 +18,10 @@ foreach (new DirectoryIterator('/some/path') as $file) {
16
18
}
17
19
```
18
20
19
-
Documentation on the `SplFileInfo` class can be found by pointing your browser here:
To make a PHP program, create a new file with a `.php` extension and start writing PHP! Execute your program by running it with the
7
7
`php` command. e.g.:
8
8
9
9
```sh
10
10
$ php program.php
11
11
```
12
+
{{ cli }}
13
+
14
+
{{ cloud }}
15
+
16
+
We've created you an empty file, look in the file tree for `solution.php`. That's your starting point.
17
+
18
+
We'll execute your solution file for you when you press the `Run` or `Verify` buttons. The `Run` button simply runs your program and captures the output, displaying it for you to view. The `Verify` button runs your program but performs some extra tasks, such as comparing the output, checking for certain structures and function calls, etc. It then displays the result of those verifications.
19
+
20
+
Both `Run` and `Verify` execute your program with random inputs which are determined by the current exercise. For example one exercise might generate a bunch of numbers to pass to your program, where another one might pass you a JSON encoded string.
21
+
22
+
{{ cloud }}
12
23
13
24
You can write to the console from a PHP program with the following code:
14
25
@@ -17,9 +28,11 @@ You can write to the console from a PHP program with the following code:
17
28
echo "text";
18
29
```
19
30
20
-
The first line tells the PHP to interpret the code following it. It is required before any PHP code is written. Read more here: [http://php.net/manual/en/language.basic-syntax.phptags.php]()
21
-
The second line is the instruction to print out some text.
31
+
The first line tells PHP to interpret the code following it. It is required before any PHP code is written. The second line is the instruction to print out some text.
32
+
33
+
{{ doc 'PHP tags' en language.basic-syntax.phptags.php }}
22
34
35
+
{{ cli }}
23
36
Place the code in to a text file using your favourite text editor, some popular editors are listed below:
24
37
25
38
* Sublime Text: [https://www.sublimetext.com/]()
@@ -30,19 +43,32 @@ Switch back to the terminal and run your code again with the same command as abo
30
43
```sh
31
44
$ php program.php
32
45
```
46
+
{{ cli }}
47
+
48
+
{{ cloud }}
49
+
Try pressing the `Run` button in the bottom right corner to execute your program.
50
+
{{ cloud }}
33
51
34
52
You should see the word "text" printed out on the console.
35
53
36
-
Now you must adapt the code to pass the challenge presented. Remember the challenge was: Write a program that prints the text "Hello World" to the console (stdout).
54
+
Now you must adapt the code to pass the presented challenge. Remember, the challenge was: Write a program that prints the text "Hello World" to the console.
37
55
56
+
{{ cli }}
38
57
We have created you a file named `hello-world.php` in your current working directory, feel free to use it!
58
+
{{ cli }}
39
59
40
-
When you have finished and saved the file you must run the following (substituting program.php to the name of the file you created which contains your code)
60
+
{{ cli }}
61
+
When you have finished editing your program and saved the file you must run the following (substituting program.php to the name of the file you created which contains your code)
41
62
42
63
```sh
43
64
$ {appname} verify program.php
44
65
```
66
+
{{ cli }}
67
+
68
+
{{ cloud }}
69
+
When you have finished editing your program you must verify it. Click the `Verify` button in the bottom right corner.
70
+
{{ cloud }}
45
71
46
-
to proceed. Your program will be tested, a report will be generated, and the lesson will be marked 'completed' if you are successful.
72
+
Your program will be tested, a report will be generated, and the lesson will be marked 'completed' if you are successful.
0 commit comments