Learning PHP in 2024
Tags: php, laravel, react, webdev
Published: 2024-03-20 17:28:31

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](https://twitter.com/jeffrey_way) for their amazing [PHP for Beginners](https://youtu.be/U2lQWR6uIuo&list=PL3VM-unCzF8ipG50KDjnzhugceoSG3RTC) 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](https://github.com/jvzaniolo/client-manager/blob/main/docker-compose.yml) 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](https://stackoverflow.com/a/9715164), 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](https://www.php.net/manual/en/pdo.drivers.php), without changing how you interact with your database.
It is _not_ an ORM nor a Query Builder.

## Folder structure

The [php-pds/skeleton](https://github.com/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
<?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:

```php
<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](https://laravel.com/docs/11.x/frontend) 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](https://marketplace.visualstudio.com/items?itemName=bmewburn.vscode-intelephense-client) 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](https://marketplace.visualstudio.com/items?itemName=open-southeners.laravel-pint).

```
composer require laravel/pint --dev
```

## Debugging

The [PHP Debug](https://marketplace.visualstudio.com/items?itemName=xdebug.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:

- [Laracasts](https://laracasts.com/)
- [PHP: The Right Way](https://phptherightway.com/)
- [PHP for Beginners (2023 Edition)](https://youtu.be/U2lQWR6uIuo&list=PL3VM-unCzF8ipG50KDjnzhugceoSG3RTC)
- [r/PHPHelp: A site to learn PHP with latest and best standard practices](https://www.reddit.com/r/PHPhelp/comments/1bfb4jd/a_site_to_learn_php_with_latest_and_best_standard/)
- [Learn PHP The Right Way - Full PHP Tutorial For Beginners & Advanced](https://youtube.com/playlist?list=PLr3d3QYzkw2xabQRUpcZ_IBk9W50M9pe-&si=1qez-gCioTtx3nag)