Hacker News new | ask | show | jobs
by meowster 2038 days ago
How should one start learning with PHP these days?

My experience: hacked together a few PHP scripts over the years to notify me of a website change (5 minute cron job). Before that, I was a very good beginner with VB6 back in highschool.

There are a couple of ambitious database-driven website projects I would like to create, but I don't know where to start. I like the KISS philosophy, and I think PHP and MySQL would fill that. Is there an obvious alternative that I'm missing?

I was thinking of hoing through the w3schools PHP turtorial. Is there a better or more up-to-date resource?

8 comments

It hasn't been updated since April 2020 but this website used to be a good resource for "modern PHP":

https://phptherightway.com/

Modern PHPs shining light is the Laravel framework and the ecosystem built up around it. If you're going to start a database-driven website projects in PHP there really isn't a good reason to not use and learn Laravel (or Lumen if you want something more lightweight).

That being said if you're not tied to PHP I'm not sure I'd necessarily recommend it. The obvious alternatives worth looking into are Ruby on Rails or Python with Flask or Django.

> PHP there really isn't a good reason to not use and learn Laravel (or Lumen if you want something more lightweight).

12 year professional PHP developer (in that it's been my main language, I've also written a lot of C# and TypeScript in that period) - I'd pick Symfony for any project I knew was going to last more than a few years before the inevitable re-write.

The problem is you don't see the difference until you've seen multiple large codebases implemented in both frameworks.

Symfony is harder to bootstrap a project with (though not really that true since 5) but in nearly every other way it gets more right than wrong.

Laravel is good and bad for the ecosystem at the same time. I've interviewed a lot of people lately who know laravel but not php. Similarly to people who claim they know react but fail in the most basic js questions.
Additionally Laravel abuses patterns like singletons and in general static methods. This is really a no-no for modern PHP, in my opinion. I think Symfony had a way better impact on the ecosystem.
To be fair, Laravel’s facades look like static method calls, but they’re actually not. They’re backed by the service container and can be swapped out for mocks etc just as easily as injected dependencies. They’re also totally optional, and in general are falling out of fashion within the community.
I could just never get used to Ruby syntax and how wordy it is.

PHP/C/perl-style syntax just feels much nicer/more natural to write and I've never been able to break out of that.

I guess it comes down to personal preference and what you started coding with.

Ruby "wordy" compared with PHP? You have to be joking. PHP is pseudo-Java in terms of verbosity. It's even worse than Java with its culture of space-wasting blank lines and fanfold doc comments.
Is Laravel better than Symfony? When I used PHP, the Symfony components were the best in the ecosystem.
"Better" is relative. Laravel is built on top of Symfony components FWIW.
> How should one start learning with PHP these days?

there is learning PHP and its standard lib, learning how to avoid all the PHP pitfalls (because there are a lot of them when it comes to webdevelopment) then learning some mandatory framework because that's how you get an actual PHP job. These 3 things are separate tasks.

For the first frankly it's the documentation + stackoverflow, the second is https://phptherightway.com/ , the third is the documentation of whatever framework you are using. Final step is to learn how to use whatever server that will front your PHP app, Apache or Nginx.

>w3schools PHP tutorial

I think a better approach is to think of a (very basic) project you want to create, and then claw and bite your way through it, until you have something that works. Then think of a way to enhance it, and go though the process again. Slogging through a tutorial is boring, and you have nothing to show for it when you're done.

PS: Don't listen to sneaky JavaScript evangelists. Wicked! Tricksy! False!

Lol, I appreciate everyone's advice, and I'll have to do research on it all to see what will work best for me.

I'm just reminded of Derek Siver's experience where he started CD Baby with PHP, and then tried rewriting it in order to use something other than PHP just because, but ended up sticking with PHP. Though he is a musician and not a professional programmer. However I'm not a professional programmer either, and I don't really want to become a sys admin to maintain the servers etc.

Agreed, that's how I learned. Also run it by someone that knows PHP so they can yell at you. I've used prepared statements ever since then
Have you considered SQLite? PHP has support for it built-in (php-sqlite3 package in Debian) and you wouldn't have to manage a database server. Concurrent requests should be fine since SQLite does R/W locking and supports transactions.

I can't personally comment on how well it works for websites but I have used PHP+SQLite for local ad-hoc GUI apps and it was a pretty nice experience.

My recommendation is to become familiar with the package manager composer

https://getcomposer.org/

Almost all community code today is distributed with composer, anything from libraries to frameworks. Regardless if you write almost everything yourself or use a full featured framework, composer is always good to learn.

And even when you are a follower of KISS there always things that you don't care enough about to write yourself, boring things like logging frameworks (Monolog https://seldaek.github.io/monolog/) or testing frameworks (PHPUnit https://phpunit.de/). And composer can also help you set up class autoloading, a must for modern PHP.

Here you can find packages to download with composer https://packagist.org/

Next I would look into different frameworks, not necessary to use them but to learn about them and from them. The 2 biggest today is Symfony ( (https://symfony.com/) and Laravel (https://laravel.com/). But there are others like Slim (https://www.slimframework.com/), CakePHP (https://cakephp.org/), Zend (https://framework.zend.com/) and many more. Try a few and get the feeling how they do things and if you like them.

For view rendering you can do that with PHP out of the box (just remember to escape output!) or you can use a template engine like the more advanced Twig (https://twig.symfony.com/) or the simpler Mustache (https://github.com/bobthecow/mustache.php). Plates (http://platesphp.com/) uses native PHP templates but helps you create reusable layouts. Some frameworks include a template engine, e.g. Laravel uses Blade https://laravel.com/docs/8.x/blade.

For database queries I recommend reading this excellent article

https://phpdelusions.net/pdo

Many frameworks include their own query builders and sometimes a database layer (ORM).

Some semi famous people within the PHP community just released a PHP 8 book, I have not read it though.

https://front-line-php.com/

They also have free videos about PHP 8

https://spatie.be/videos/front-line-php

Ask for help https://www.reddit.com/r/PHPhelp/

Discussions about PHP https://www.reddit.com/r/PHP/

Online PHP shell https://3v4l.org/

Good luck!

Thank you for the very in-depth reply!
Keep hacking, learn from your own mistakes, do this for 10 years. By the end of that time you should, for example, understand why we use Postgres instead of MySQL. :)
Every language I think about is better than PHP (Javascript, Python, Ruby). Personally I will choose Javascript / Node JS platform to start with.
why? Those three come with the same amount of quirkiness and issues. There is a reason why you have Typescript or golang, for example.
Typescript is just a much improved javascript, to the point of these two just being two dialects of the same language. I wouldn't make a distinction between the two. Performance-wise, as a servrr-side language, they both have the same strengths and weaknesses.