Hacker News new | past | comments | ask | show | jobs | submit login
PHP Apprentice – An online, open-source book for learning PHP (phpapprentice.com)
268 points by restoreddev 3 months ago | hide | past | web | favorite | 70 comments

The content is great. I like how you start off teaching the language by showing how to execute simple programs from the command line (I learned that you don’t need the `-f` option to execute a file). I’ve come across far too many PHP introductions that confuse the new learner by attempting to get them to set up a web server (usually Apache) and then all the strings have to be wrapped in HTML markup when printed. There’s a lot to be said for keeping it simple for teaching the essentials of a language.

The site is cleanly and beautifully formatted and I love how lightweight it is: HTML files, a CSS file, a JS file and an image. It’s such a refreshing change from the thousands of visually cluttered sites that make hundreds of resource requests (most of them third-party) and whose content is not visible without allowing third-party JavaScript. When I completely disabled JavaScript on this site, all I noticed were non-essentials such as Solarized syntax highlighting and the Table of Contents; I could still navigate through the site using the Previous and Next links (and of course, the browser’s Back button).


Thanks! I really made it a goal to keep the site lightweight and simple.

I can't understand sites without a fallback so many are turning javascript off.

Nice job. I would suggest all PHP devs spend some time with https://phptherightway.com. Your resource looks to be a great starting point.

Not disagreeing but I usually recommend that people read http://www.phpthewrongway.com/ afterwards to get a more pragmatic viewpoint.

To be honest, it reads more as someone being salty they don’t understand the point of frameworks.

I think you skimmed the article too quick then because there are plenty of good and well supported arguments in it.

I think I read mostly to the end before giving up on it.

There’s a lot of arguments, I just don’t think they’re particularly good.

Don’t misunderstand me, there is a point there, but it’s buried under a load of text.

I regularly use bash and I have learnt HTML, CSS and JavaScript in the past. If I had to use only one of these resources - OP's site or PHP the Right Way - which would you recommend I use? To put it some other ways - does OP's site adequately cover best practices? Is PHP the Right Way targeted aimed at fully-fledged programmers who want to pick up PHP?

Thank you in advance, it's much appreciated.

Thanks for reading! This is still a young project with room for improvement and new sections. I’m already planning some content for building web pages, database access and package management with Composer. I wrote an announcement post over on dev.to if you want to read more about how the site was built: https://dev.to/restoreddev/announcing-php-apprentice-coc

Very well done, there’s a lot to help a novice PHP developer. I feel like the next chapters I’d love to see would take those concepts and apply them to building something. Everything so far unless I missed it is focused on the backend but it’s important to show that it’s also possible to use it as a front end and what the modern approaches to doing that are.

Thats a good point. PHP is a general language, but used almost exclusively for web development and most websites now use a framework (symfony/laravel......) etc.

I'm learning symfony framework after my micro framework of choice lost its support (Silex..).

The symfony documents are ok, but I wouldn't want to give them to someone starting out. (though looking again, they seem better..) https://symfony.com/doc/current/page_creation.html

Someone showed me the free video cast howto build a basic symfony site. I though it was pretty good. Like many of these, the basic tutorials are free, but its a subscription site.


I plan to add a new section for learning how to use the concepts to build a basic website. I wanted to start with backend foundation first.

Nice job and well written. A nice addition would be how to use pdo. I'm appalled by the number of ressources I see online which are using the old mysql api.

> I'm appalled by the number of ressources I see online which are using the old mysql api

Recently authored articles? We have an internet filled with 20 years of PHP tutorials, and unfortunately for beginners there's not an easy way to evaluate if something utilizes modern best practices or is an Experts Exchange post from 2001.

I find strange you can't down-vote a link on Google, it should exist and Google should take it into account specially for technical links (less so for opinions articles or anything political)

Everything is political. I wouldn't want people to downvote Mercurial links into oblivion.

Anyway, Google links are pretty much already ordered by popularity.

That's the problem, in technical links popularity (as in number of visits) is not linked to correctness, that's one of the reasons StackOverflow is so useful, because the top-voted answer is like 99% of the times the most correct (useful) one.

Except when that answer is 5 years old and is as wrong as the ExpertsExchange post from 2001.

Unlike EE, though, any user can propose an edit to that now-incorrect answer (or just edit it outright, if they have 2k rep).

A nice big "this answer is outdated, see [other answer]" note does wonders.

Unfortunately, such edits are rarely accepted. The intent was for the site to be more wiki-like, but these days "respect the author's intent" is the prevailing wisdom. The cases where someone edits a top answer with a significant correction or update are very few, and require overwhelming community support or they'll be reverted.

This is particularly distressing in the case of security vulnerabilities. Vulnerable code being copied from Stack Overflow into consumer applications isn't hypothetical, it's been empirically demonstrated (https://www.aisec.fraunhofer.de/en/stackoverflow.html), yet the community and company refuse to accept responsibility and address the problem. (Of course the developer creating the application is more responsible, but Stack Overflow is in a position where they could help protect innocent end users and the common good.)

Pinning of accepted answers to the top of the list was a good idea when the site was young. These days, it prevents updated better answers from taking the place of ten-year-old misinformation.

Built-in SEO gaming would be a weird feature

Yep, the plethora of out-of-date resources is what I'd place as the main issue with PHP today. I usually steer people to use the Laravel framework since it makes it difficult to write bad/dangerous code.

This is a very good resource for PDO: https://phpdelusions.net/pdo

https://phpdelusions.net/pdo has done a very thorough job of covering PDO IMO and makes a fantastic companion to https://phptherightway.com/.

PDO is definitely on the list. There are very few resources for it.

Can confirm. I've written one site with PDOs and a lot of sites with the old stuff. PDO is definitely a different feel to wrap around, and more resources on it would definitely be good.

For what it's worth though, my understanding is mysql_ functions are removed completely as of PHP 7. So that old documentation will just not work, rather than teaching people the least secure option of how to write their website.

You're right, but you can just replace mysql_* with mysqli_* if you're so inclined.

that doesn't work.

mysql_query($sql, $link);

mysqli_query($link, $sql);

I’ appalled that my 10 year old website recently suddenly broke because someone updated PHP to 7.2.

Then I realized it was pretty amazing that it lasted that long in the first place.

similar. got a call from someone re: a site built on a homegrown mvc framework from 2000/2001 (when PHP4 was just out).

this call came in late 2017 saying "hey, we migrated the site and it doesn't work". There was some change in PHP5.6 and the initial PHP4 code had some "by reference" class stuff that wasn't working any more. It was a small change to get it working again, but there was enough PHP4 in it that a move to PHP7 wasn't going to work without a lot of work. I'd suggested a rebuild, which... wasn't too well received. Pointing out that they'd had 16 years of functionality on a web codebase (with no other costs) was probably as much ROI/value as you'd ever be able to get out of anything.

I haven't used PHP in around 6 years (running PHP 4 I belive); has the language improved ? My biggest complaint was that the API was incredibly inconsistent, but that seemed like it could be improved in later versions if they weren't afraid of breaking backwards compatibility.

One of the best things of PHP today is the ecosystem. The PSR [0] interfaces let you build your own framework using libraries from the major frameworks or independent libraries seamlessly.

[0] https://www.php-fig.org/psr/

I have been using PHP for a new job after not using it since PHP 4 too but I was also a very immature dev back then. I quite like it. It's not perfect and it still lacks some things but it's very easy to put something together. I think that my biggest problem is actually that it's too easy, which makes some people write code that it's a bit too sloppy and hard to understand, creating a possibly huge technical dept.

PHP 7+ is a totally different world from 4. It’s actually a real programming language now. The massive API redundancy is still there, but you have classes, namespacing, etc.

Also adding libsodium directly into PHP core as of 7.2 was a big step in the right direction IMO.

Yes, including today's merge of property types:

    class A {
        public int $id = 0;
        public ?Foo $somefoo;
The language has evolved slowly but surely, and it's now much harder to make obvious mistakes thanks to a bunch of good-quality static analysis tools (one of which I authored).

There have been a number of improvements in the latest PHP versions, especially in terms of performance.

There's still artifacts of past PHP versions that are there, though, which makes it far less nice to use than something more modern.

It was likely PHP 5. The last PHP 4 release is more than 10 years old [1].

Later versions of PHP 5 were worlds apart from version 4.

[1] http://php.net/supported-versions.php

I'm pretty sure it was 4, the company I was working for was...let's just say "slow to update", which is partly why I left.

It has improved a lot in that time and I find it decent to work with now that there's decent dependency management with Composer and a good Rails-like framework in Laravel.

PHP 7 is a very different (better) language compared to 4. You can write very good quality code with it now.

I love PHP, it was my first “real” programming language, and I’m glad there are still people trying to make it easier to adopt.

Having skimmed through several of these chapters, though, I just feel like it’s too similar to the official PHP docs, even down to the TOC.

Wouldn’t it have been better for everyone to put this effort into improving the docs, rather than developing yet another book in a silo that likely won’t have the longevity we’d like?

I started with foundational concepts which will overlap some with the official docs. The advantage of my site is I wanted it to be more readable, since the official docs can be hard to navigate. In the future, I want to cover topics like Composer and Slim which would not be appropriate for PHP.net.

That’s cool and does give the comprehensive approach some credence, but still feels like duplicated effort and the potential for outdated content (on both sides).

FWIW, I don’t think giving a more readable aspect to the official docs would be bad. It’s already got way more of that feel than it did in PHP 4, when it had more of an MSDN auto-generated parameter documentation feel. With the clear sections and code snippets I think both could be achieved with little extra effort.

Imagine this kind of reception for any other content. Someone spends a year into composing a new symphony and the reviews go "I love live music but wouldn't it have been better if the composer would have re-orchestrated a familiar classic rather than developing a whole new score that won't be performed as often as we'd like?"

That analogy makes no sense.

Are there other technical reason's for advocating it, other than your love because it was your first "real" language?

I can't find a better language to use in 2019 in the backend if you are developing a website.

Is there a better language built for rapid web application development. You could debate which is better rails/laravel but in 2019 I would learn php.

You may suggest Go, python, .net, node, Perl or java. They could be used but each only offers an advantage in certain situations and make development slower or have another disadvantage.

If you want to make a website using php is the simplest easiest fastest choice available.

Thank you. I've looked for this sort of explanation.

I think it’s still one of the pragmatic language around.

The ‘quick and very dirty’ parts are still there, the ‘clean and efficients’ bits have been added, and there’s a ton of still runnable doc and code lying around.

All of that is a blessing and a curse of course, but I think that makes it a unique proposition.

The null coalescing operator ?? is not well explained. It returns the first operand but if it's not set or null, will return the second operand, i.e:

    $name = $_GET['name'] ?? $_POST['name'] ?? "unknown";
In the arrays section, I'd also introduce the $array[] shorthand for array_push(), i.e:

    $array[] = $new_element;

Looks great! Perhaps, making it possible to turn pages with the arrow keys on the keyboard would make it a bit easier to navigate.

Yeah, that was my first reaction as well! With the chapters being this short (which itself is great) it would be really nice to be able to use the arrow keys.

Not sure if the dev will see this, but it would be nice if it was a little easier to get to the table of contents as well. My suggested code change. (quick a dirty example)

  .menu {
    margin-bottom: 1.5em;
    position: sticky;
    top: 0;
    z-index: 1000;
    background: #fff;
    padding: 10px;

FYI, just implemented this on the site. Thanks for the suggestion!

Noted! That would be an easy addition.

Nice little reference for anyone who already knows how to program, but might not know PHP. For anyone else, this isn't really going to help. There seems to be no explanation of _why_ you would want to do anything here or even how it relates to a goal. Who is your audience?

IMHO, I think the audience is me. I can program (perl, java, c++). I know that php is easy but that there are many traps. I need a book to be able to develop quickly with sane practices. The official documentation contains too much old crust.

Too much old crust, but as other commenters around indicate, this isn't really solving that either: it doesn't talk about best practices or even highlight where the sharp edges are (EOLs and Namespaces are mentioned elsewhere).

This was my same takeaway thumbing through. I think I'll take the weekend to grok PHP a bit better.

Thanks for publishing this op!

Good job ! Please add something about namespaces.

It’s on the roadmap!

And composer!

Looks like good material and the formatting is nice so kudos.

Well written and easy to read- hugely valuable resource! Can't wait to see more.

Why not teach it better? https://glot.io/snippets/f8g0wlar0d

No wonder PHP developers have a bad reputation.

I downvoted you because your comment doesn't help anybody and is offensive. Why do you think your example is better?

Because the examples given in this "book" teach a bad coding style and explain nothing in detail. If a trainee only used this to prepare for the final exams and I was to ask a more in depth question he would propably fail.

Example: "Why do you use double quotes everywhere?" Most probable answer: "Because I can use variables and the backslash-n thingies everywhere and its more convinient than using single quotes."

Example: "Why do you use '. "\n";' instead of the PHP_EOL constant?" Most probable answer: "What is PHP_EOL?";

Example: "Why aren't you using namespaces anywhere?" Most probable answer: "What are namespaces?"

Altough this might be a german perspective but I would not hire someone that presents me this kind of code. And for anyone saying "But this is only one book to learn the basics?" why not learn it the right way from the start?

Not sure I follow. What are your criticisms, specifically?

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact