Learning PHP in 2024

jvzaniolo

João Vitor

Posted on March 20, 2024

Learning PHP in 2024

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

💖 💪 🙅 🚩
jvzaniolo
João Vitor

Posted on March 20, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Learning PHP in 2024
php Learning PHP in 2024

March 20, 2024