├── tutorials
├── README.md
├── Advanced
│ └── readme.md
├── Intermediate
│ └── readme.md
└── Novice
│ └── CommentTutorial.md
├── exercises
├── Advanced
│ └── readme.md
├── Intermediate
│ └── readme.md
├── Novice
│ ├── RatingExercise
│ │ ├── assets
│ │ │ ├── star_empty.png
│ │ │ ├── star_full.png
│ │ │ └── star_partial.png
│ │ ├── RatingSolution5.php
│ │ ├── RatingSolution4.php
│ │ ├── RatingSolution1.php
│ │ ├── RatingExercise.md
│ │ ├── RatingSolution2.php
│ │ └── RatingSolution3.php
│ ├── ArraySearchExercise
│ │ ├── assets
│ │ │ └── sunhours.php
│ │ ├── ArraySearchSolution1.php
│ │ └── ArraySearchExercise.md
│ └── SequenceExercise
│ │ ├── SequenceSolution1.php
│ │ └── SequenceExercise.md
└── README.md
├── .gitignore
├── resources
├── README.md
├── Books.txt
├── Licenses.txt
├── Essential Blogs.txt
├── Tutorials.txt
├── Video_and_Podcasts.txt
└── GrumpyProgrammersGuideToMentoring.txt
├── spotlight
└── README.md
└── README.md
/tutorials/README.md:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/exercises/Advanced/readme.md:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tutorials/Advanced/readme.md:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /nbproject/private/
--------------------------------------------------------------------------------
/exercises/Intermediate/readme.md:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tutorials/Intermediate/readme.md:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/resources/README.md:
--------------------------------------------------------------------------------
1 | Resources
2 | =============
3 |
4 | Here we aim to list resources aimed at complete beginner to novice level PHP programmers.
5 |
--------------------------------------------------------------------------------
/spotlight/README.md:
--------------------------------------------------------------------------------
1 | Spotlight
2 | =============
3 |
4 | Here you will find code focussing on individual PHP functions and examples of their use (or misuse).
5 |
--------------------------------------------------------------------------------
/exercises/Novice/RatingExercise/assets/star_empty.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/phpmentoring/resources-php/HEAD/exercises/Novice/RatingExercise/assets/star_empty.png
--------------------------------------------------------------------------------
/exercises/Novice/RatingExercise/assets/star_full.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/phpmentoring/resources-php/HEAD/exercises/Novice/RatingExercise/assets/star_full.png
--------------------------------------------------------------------------------
/exercises/Novice/RatingExercise/assets/star_partial.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/phpmentoring/resources-php/HEAD/exercises/Novice/RatingExercise/assets/star_partial.png
--------------------------------------------------------------------------------
/resources/Books.txt:
--------------------------------------------------------------------------------
1 | PHP Master* http://www.sitepoint.com/books/phppro1/
2 |
3 | PHP Architect http://www.phparch.com/
4 | (magazine)
5 |
6 |
7 |
8 |
9 | *not suitable for absolute beginners, but should provide a good challenge for novice developers
--------------------------------------------------------------------------------
/exercises/Novice/ArraySearchExercise/assets/sunhours.php:
--------------------------------------------------------------------------------
1 | = 1) {
9 | $stars--;
10 | $count--;
11 | $output .= "Full Star ";
12 | }
13 |
14 | if ($count > 0) {
15 | $stars--;
16 | $output .= "Partial Star ";
17 | }
18 |
19 | while ($stars > 0) {
20 | $stars--;
21 | $output .= "Empty Star ";
22 | }
23 |
24 | echo $output . "($rating)\n";
25 |
--------------------------------------------------------------------------------
/exercises/Novice/ArraySearchExercise/ArraySearchSolution1.php:
--------------------------------------------------------------------------------
1 | = 1)
8 | $str .= "
";
9 | else if ($a > 0) {
10 | $str .= "
";
11 | }
12 | } while (floor($a) > 0);
13 |
14 | for ($i = 0; $i < 5 - ceil($rating); $i++)
15 | $str .= "
";
16 |
17 | echo $str . " " . $rating;
18 | ?>
19 |
--------------------------------------------------------------------------------
/resources/Video_and_Podcasts.txt:
--------------------------------------------------------------------------------
1 | ProTalk http://protalk.me
2 |
3 | A community site that provides a central point of access to audio/ video content
4 | with a PHP focus. You can expect to find talks from all the big conferences (those
5 | that record sessions that is) and podcasts from the following excellent series':
6 |
7 | Voices of the Elephpant http://www.elephantvoices.org/
8 | DevHell http://devhell.info/
9 | DPC Radio http://blog.ibuildings.com/dpc-radio/
10 |
11 | ...and many more.
12 |
13 | ProTalk is a brand new site with new features and content being added all the time.
--------------------------------------------------------------------------------
/exercises/README.md:
--------------------------------------------------------------------------------
1 | Exercises
2 | =============
3 |
4 | Each exercise lives in a self-contained directory. Exercise directories contain at least one exercise file
5 | and one solution file. Some may contain an assets directory that pertain to the exercise. There is often
6 | more than one way to solve a problem in programming, hence it will not be unusual to discover more than
7 | one solution file for an exercise.
8 |
9 | Contributions
10 | -------------
11 |
12 | If you would like to contribute an exercise, or your own solution to an existing exercise for others to learn from, please open a pull request.
13 |
14 | Please place your contribution in the appropriate sub-directory, according to difficulty. Thank you.
15 |
16 |
--------------------------------------------------------------------------------
/exercises/Novice/SequenceExercise/SequenceExercise.md:
--------------------------------------------------------------------------------
1 | Level 4 Sequence Exercise
2 | -----------------------
3 | Create a function which returns the xp for a given level. Use the following sequence for the algorithm.
4 |
5 |
6 |
7 | | xp | level |
8 |
9 |
10 |
11 | | 100 | 1 |
12 | | 300 | 2 |
13 | | 600 | 3 |
14 | | 1000 | 4 |
15 | | 1500 | 5 |
16 |
17 |
18 |
19 |
20 | ##Rules:
21 | * the first param of the function may be the level
22 |
23 | ##Solution:
24 | See [L4_SequenceSolution1.php](https://github.com/mindyk/beginning-php/blob/master/exercises/L4_SequenceExercise/L4_SequenceSolution1.php)
25 |
26 | ##Author:
27 | This exercise was submitted by mindyk
--------------------------------------------------------------------------------
/exercises/Novice/ArraySearchExercise/ArraySearchExercise.md:
--------------------------------------------------------------------------------
1 | Level 4 ArraySearch Exercise
2 | -----------------------
3 | Create an algorithm for the function "getSunHours", which will return the sun hours by zipcode from the 2 dimensional array "aSunHours". The Param 1 from "getSunHours" has to be the zipcode.
4 | see [sunhours.php](https://github.com/mindyk/beginning-php/blob/master/exercises/L4_ArraySearchExercise/assets/sunhours.php)
5 |
6 | ##Rules:
7 | * aSunHours is sorted by zipcode ASC
8 | * If the passed zipcode is not in array the function may return the sun hours from next smaller zipcode
9 | * If the passed zipcode is smaller then the smallest zipcode in array you may return -1
10 |
11 | ##Solution:
12 | See [L4_ArraySearchSolution1.php](https://github.com/mindyk/beginning-php/blob/master/exercises/L4_ArraySearchExercise/L4_ArraySearchSolution1.php)
13 |
14 | ##Author:
15 | This exercise was submitted by mindyk
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Resources for PHP
2 | =============
3 |
4 | This repository contains PHP code examples, exercises and tutorials for novice, intermediate and advanced developers.
5 |
6 |
7 | Contributions
8 | -------------
9 |
10 | If you have any code you would like to contribute to this repository, please feel free to open a pull request. Please keep the code pure PHP so as not to detract from the learning experience.
11 |
12 | Please place your contribution in the appropriate sub-directory (where available), according to difficulty.
13 |
14 |
15 | Feedback
16 | --------
17 | Contributors to this repo would be very grateful to receive feedback on their hard work! If you would like to praise or comment on any item, or the repo as a whole, please do so in the issue tracker. We're keen to know what you think, so please take a moment to let us know.
18 |
19 |
20 | Contact
21 | -------
22 |
23 | If you have any questions about this repo, or need some help to contribute, please make it known on IRC freenode #phpmentoring. Thank you.
--------------------------------------------------------------------------------
/exercises/Novice/RatingExercise/RatingSolution1.php:
--------------------------------------------------------------------------------
1 | ` tags instead of strings, using the star images provided in the assets directory.
19 |
20 | ##Solution:
21 |
22 | See [L3_RatingSolution1.php](https://github.com/phpmentoring/beginning-php/blob/master/exercises/L3_RatingExercise/L3_RatingSolution1.php) and [L3_RatingSolution2.php](https://github.com/phpmentoring/beginning-php/blob/master/exercises/L3_RatingExercise/L3_RatingSolution2.php) for solutions to this exercise. Other solutions in the repo were submitted by contributors via pull request.
23 |
24 | ##Author:
25 |
26 | This exercise was submitted by Tocacar
--------------------------------------------------------------------------------
/exercises/Novice/RatingExercise/RatingSolution2.php:
--------------------------------------------------------------------------------
1 | tags using the star images in the assets directory
9 | *
10 | */
11 |
12 | function getRating()
13 | {
14 | $rating = rand(0,50) / 10;
15 |
16 | $partialStar = 0;
17 |
18 | $fullStars = floor($rating);
19 |
20 | if ($rating - $fullStars > 0) $partialStar = 1;
21 |
22 | $emptyStars = 5 - $fullStars - $partialStar;
23 |
24 | $ratingDisplay = '';
25 |
26 | for($i = 0; $i < $fullStars; $i++) {
27 | $ratingDisplay .= '
';
28 | }
29 |
30 | if ($partialStar)
31 | {
32 | $ratingDisplay .= '
';
33 | }
34 |
35 | for($i = 0; $i < $emptyStars; $i++) {
36 | $ratingDisplay .= '
';
37 | }
38 |
39 | echo $ratingDisplay . ' (' . $rating . ")";
40 | }
41 |
42 | //execute the function
43 | getRating();
44 |
45 | //refresh the page multiple times to see the output change
--------------------------------------------------------------------------------
/exercises/Novice/RatingExercise/RatingSolution3.php:
--------------------------------------------------------------------------------
1 | ';
5 | $partialStar = '
';
6 | $emptyStar = '
';
7 | $ratingArr = array($emptyStar, $emptyStar, $emptyStar, $emptyStar, $emptyStar);
8 | $str = "";
9 |
10 | $rating = mt_rand(1,50) / 10;
11 | $int = floor($rating);//how many full stars
12 | $rest = 5 - $int;//how many empty stars
13 | $dec = $rating - $int;//do we have partial star
14 |
15 | if($int >= 1){
16 |
17 | for ($i = 0; $i < $int; $i++) {//replace the empty stars with full stars in array
18 | $ratingArr[$i] = $fullStar;
19 | }
20 |
21 | if ($dec > 0) {//if we have partial stars positon after the last full star in array
22 | $ratingArr[$int] = $partialStar;
23 | }
24 |
25 | }else if ((0 < $rating) && ($rating < 1)) {//if we have subunitar rating
26 | $ratingArr[0] = $partialStar;
27 | }
28 |
29 | foreach ($ratingArr as $key => $value) {//create the string
30 | $str .= " ".$value;
31 | }
32 |
33 | return $str." ($rating)";
34 |
35 | }
36 | echo ratingExercise();
37 |
--------------------------------------------------------------------------------
/resources/GrumpyProgrammersGuideToMentoring.txt:
--------------------------------------------------------------------------------
1 | The Grumpy Programmer's Guide To Mentoring
2 |
3 | So you've decided you want to become a mentor to another programmer? Sharing
4 | your skills and knowledge with everyone is awesome, so thanks for agreeing
5 | to do something like it. Here are a few thoughts on how to make the
6 | experience awesome for both mentor and protégé.
7 |
8 | #1. It's about making someone better
9 |
10 | Clearly, you have a skill that is in demand and other people want to get
11 | better at. So your entire approach should be aimed to helping that other
12 | person build their skills up. Ask them questions about what they are
13 | having trouble with, and show extraordinary patience in explaining topics
14 | and concepts that might be obvious to you but completely new to them.
15 |
16 | #2. Regular communication
17 |
18 | I am currently mentoring 3 programmers on the finer arts of testing. I
19 | have a weekly voice-to-voice chat with them.
20 |
21 | #3. But not too much communication
22 |
23 | I tell my protégés that they can email me at any time and also give them
24 | IM account names they can reach me at, but I also made it clear that I
25 | have a day job and that will always take priority. Ad-hoc communication
26 | channels are good, but set the expectation that these boundaries need
27 | to be respected if this relationship is going to work out.
28 |
29 | #4. Build a lasting friendship
30 |
31 | In my talks I am focusing on helping them out with their testing skills
32 | but also becoming their friend in the process. The relationship between
33 | mentors and protégés can be one that lasts long after they are no longer
34 | actively working through these problems together. Learn about people
35 | and their lives outside of coding, you'd be surprised what could happen.
36 |
37 |
38 |
--------------------------------------------------------------------------------
/tutorials/Novice/CommentTutorial.md:
--------------------------------------------------------------------------------
1 | Level 1 Commenting Tutorial
2 | ---------------------------
3 |
4 | Getting comments right can be tricky when you're just starting out. A good rule of thumb is that code should be self documenting, unless its particularly complex.
5 |
6 | Well chosen class, method and property names make code more readable and easier for other developers to understand (this is what is meant by self-documenting). Sometimes, however, areas of code can be necessarily complex and it is here that comments should be used as an aid to 'future you', and other developers who may be required to maintain your code.
7 |
8 | ##Inline comments
9 |
10 | // Inline comments can be made with # or // symbols. These
11 | // symbols should appear on every new line in a multi-line
12 | // comment, as shown here.
13 |
14 | # Its good to be consistent, so you should probably pick a
15 | # symbol and stick with it.
16 |
17 |
18 | ##Block comments
19 |
20 | /*
21 | * Block comments can be wrapped by /* and */ symbols at the
22 | * start and end of the block. The text you are reading now
23 | * is inside a block comment (the use of * on each line is not
24 | * required but can make it easier to spot the block comment).
25 | */
26 |
27 |
28 | Below are some simple examples of bad, good and better use of comments, which should give you a clearer idea of when and how to use them. Follow the related reading links at the end of the page for more information.
29 |
30 |
31 |
32 | ##Bad comments
33 |
34 | // get a user by primary key
35 | $user = $this->getRepository('User')->findOneById($id);
36 |
37 | if(!$user) {
38 | // if user doesn't exist, throw an exception
39 | throw new NotFoundHttpException('No user found for id: '.$id);
40 | }
41 |
42 | These comments are 'bad' because it is not necessary to comment every line. They do not add value to the code and in fact make it a bit less readable perhaps.
43 |
44 | Also, when refactoring code it is easy to forget to update the comments. Out of date comments are worse than no comments, and ubiquitous comments can be difficult to keep up to to date.
45 |
46 |
47 |
48 | ##Better comments
49 |
50 | // confirm the user exists
51 | $user = $this->getRepository('User')->findOneById($id);
52 |
53 | if(!$user) {
54 | throw new NotFoundHttpException('No user found for id: '.$id);
55 | }
56 |
57 | This single comment is better because it is short and concise, clearly indicating the purpose of the code that follows.
58 |
59 |
60 | ##Even better?
61 |
62 | if(!userExists($id)) {
63 | throw new NotFoundHttpException('No user found for id: '.$id);
64 | }
65 |
66 | /**
67 | * Checks for an existing user.
68 | *
69 | * @return bool
70 | */
71 | function userExists($id)
72 | {
73 | $user = $this->getRepository('User')->findOneById($id);
74 |
75 | if(!$user) {
76 | return false;
77 | }
78 |
79 | return true;
80 | }
81 |
82 | This example is 'even better' because the code is cleaner and the block comment provided describes the userExists method clearly and concisely, indicating the expected return type.
83 |
84 |
85 |
86 | ## Accreditation
87 | Code samples for this tutorial were provided by [@lazymanc](http://www.wheelsandcogs.co.uk)
88 |
89 |
90 | ## Related reading:
91 |
92 | [http://www.codinghorror.com/blog/2006/12/code-tells-you-how-comments-tell-you-why.html](http://www.codinghorror.com/blog/2006/12/code-tells-you-how-comments-tell-you-why.html)
93 |
94 | [http://www.codinghorror.com/blog/2008/07/coding-without-comments.html](http://www.codinghorror.com/blog/2008/07/coding-without-comments.html)
95 |
96 |
--------------------------------------------------------------------------------