A quick introduction to PHP development in 2024, with useful links and resources.
Intro
I was recently asked to do an interview code challenge using pure PHP, but the content I found online was outdated and didn't talk about modern PHP development.
In this post, I'll try to share what I've learned about PHP so far.
Before we begin
Coming from a React and Next.js background I never really had the chance to understand how things work in web browsers.
It was not until I started working with Remix, that I realized how great your code can be when you build on top of web standards.
Using PHP is closer to Remix than React SPA, without the JS benefits.
But I'm not here to talk about Remix (although if you're into React, you should try it!).
Here are some of the things I found when coding this challenge.
Outdated resources
Since PHP has been around for decades, most of the information I've found online was not up-to-date with its recent versions and modern developer tools, so it took me a while to find good resources.
I'll link most of them at the end of this post, but I want to shout out to Laracasts and Jeffrey Way for their amazing PHP for Beginners series on YouTube.
Getting started
If you have Homebrew installed, install php with
brew install php
Use the built-in server
PHP now comes with a built-in server that you can use to serve your application. No need for Nginx or Apache for local development.
For example:
php -S localhost:8888
This will serve your current directory on http://localhost:8888
No local database
Use Docker as an alternative to installing a database locally.
Check my docker compose file here for a good starting point. It will start a MySQL container on port 3306
.
Notice the initdb.sql
file. Copying it over to the docker-entrypoint-initdb.d/
folder will run the SQL file once you run docker-compose up -d
.
This is useful for creating tables and seeding the database.
The connection string will look mostly like this:
"mysql:host=127.0.0.1;port=3306;dbname=myapp;user=root;password=my-secret-pw;charset=utf8mb4"
Use host 127.0.0.1
instead of localhost
, to connect with TCP instead of UNIX socket, otherwise it will throw a PDOException [2002].
Using PDO
PDO (PHP Data Objects) is a consistent interface for accessing databases in PHP.
It makes it possible to swap MySQL for any other available driver, without changing how you interact with your database.
It is not an ORM nor a Query Builder.
Folder structure
The php-pds/skeleton repository is a great document to learn how to structure your code in PHP, but it doesn't show how to organize your code inside the src/
folder.
I recommend starting a basic Laravel project to see how they structure Routes, HTTP Handlers, Authentication, etc.
Types
PHP is by default a dynamically typed language, but you can ensure strict types per file with the following:
<?php
define(strict_types=1);
Strict type support was introduced in PHP 7, making it easier for developers to catch bugs in code that pass the wrong types of values to strict functions.
Composer
Composer is a dependency manager for PHP, much like Node's npm
.
You can install dependencies with composer require
and run scripts with composer <script>
.
It installs dependencies to a vendor/
folder, which you should include in your .gitingore
file.
Templating
PHP is a templating language but doesn't have the best ergonomics.
Take this loop for example:
<ul>
<?php foreach($users as $user): ?>
<!-- `echo` prints the variable to HTML -->
<!-- `htmlspecialchars` secures against XSS attacks -->
<li><?php echo htmlspecialchars($user['name']) ?></li>
<?php endforeach; ?>
</ul>
That's where templating libraries come to the rescue.
Templating libraries
There are two types of templating libs to choose from:
- Native (Plates, Aura.View)
- Compiled (Twig, Latte, Smarty)
Native libraries are easier to learn because they use PHP syntax and run on .php
files, so your code gets syntax highlighting.
Compiled libraries have somewhat different syntax, but less code to write, and automatic HTML escaping.
If you use Laravel, the default templating library is Blade but there are options to run React, Vue, and Svelte code too!
Check the Laravel frontend docs to learn all the available options.
Laravel
If you don't need to use pure PHP like I had to, I highly recommend you try Laravel.
Laravel is an amazing web framework with everything you need to build an enterprise-ready application.
VSCode integration
The PHP Intelephense extension has many features to ease the developer experience in PHP.
It also has a code formatter, but if you want more customization, check out Laravel Pint.
composer require laravel/pint --dev
Debugging
The PHP Debug extension adds support for debugging with Xdebug on VSCode.
Follow its instructions to install Xdebug and create the .vscode/launch.json
file.
It's also useful to install the "Xdebug Helper" browser extension.
Resources
These are a MUST if you're learning PHP in 2024:
Top comments (3)
I find it great to see a javascript developer that is willing to learn a general purpose language. And it is my comfort language 😄
Php or any other script language, python/ruby, is a good way to ease into general purpose languages. Because they don't require compiling or force typing.
To keep you computer as clean as possible, I would not recommend to install php. Use a virtual machine setup, like xampp or wampp, or docker front-ends, like ddev or lando.
While Paul M. Jones is a great asset for the PHP community, I would not use the skeleton. If you are not creating a framework based project, just follow the composer package basics.
It might be a little scary not having a more defined structure. But you know you project best, so you are best equipped how to structure the code.
Laravel could be a big change if you come from javascript. If you want a framework that has less restrictions use the slim framework. It will require a little more setup because it has no template engine or ORM out of the box. So only use it in production once you know what you are doing to prevent attacks like XSS, sql injection, and so on. Framework like Laravel have counter measures build in.
I would like to shout out php.net as a great resource for news, changelogs, upgrade documentation and general documentation for PHP. And as for the frameworks out there I would also recommend a look at Symfony, a very well developed framework with a pretty long history behind it.
Good, but to develop in PHP you have to use PHPStorm: nothing is better.