PHP

If you're completely new to PHP, GitHub's Learning Lab [Introduction to PHP][] is a good place to build a solid foundation.

[Introduction to PHP]:

First things first, you'll need to install PHP

PHP and Docker

PHP has a lot of dependencies, causing some to prefer running PHP in a container. You can use Docker in Phpstorm, which I learned to do by following their post "Quickstart with Docker in PhpStorm which included both an article and a video. The containers recommended by PhpStorm for use with zero-configuration debugging can be found in the PhpStorm Docker registry

Configuration

To add custom configurations to your PHP environment, you'll need to modify the PHP configuration file: php.ini. To set a custom location for the php.ini file, set the PHPRC environment variable to whatever filepath you'd like your php.ini file to be located.

Extensions

Printing values

Echo

I learned reading the PHP documentation on echo that echo has a shortcut syntax.

I have <?=$foo?> foo.

Heredocs

I recommend reading the PHP documentation on strings they explain it far better than I could

MySQLi

PhpStorm

I'm not sure if this is always the case, but when I created a Docker container php:rc-apache in PhpStorm, it deployed to port 32771 on my local machine. It's easier if I just have port 80 route directly to the Docker container of the PHP server.

To fix this, update the Run/Debug configurations for the Docker application. The final command should look like this:

docker run \
    -P \
    -v ${PWD}:/var/www/html \
    --name 'myphpcontainer' \
    -p 80:80 \
    --sysctl net.ipv4.ip_unprivileged_port_start=0 \
    php:rc-apache

Debugging PHP with xdebug

You should really use a debugger when working on projects that use PHP. I mean, echoing var_dump and var_export can only get you so far. For this reason, I recommend Xdebug, which seems to be the most popular debugger. To download it, check out the Xdebug installation guide, or just copy the command below:

Next, to edit PHP, I recommend using Jetbrain's IDE for PHP: PhpStorm. It's worth noting that you can set up Xdebug on PhpStorm. I found the JetBrainsTV PhpStorm Video Tutorial helpful for setting up the debugger in my IDE. To get xdebug up and running, you'll need to add a section titled [xdebug] to your php.ini configuration file.

[xdebug]
zend_extension="/usr/local/opt/php@7.4/pecl/20190902/xdebug.so"
; Turns on remote debugging across the board
xdebug.remote_enable=on
; Whatever IP address makes the request, send the request back to that host
; This works whether it's local, remote, or a Docker container
xdebug.remote_connect_back=on
; xdebug runs on port 9000
xdebug.remote_port = 9000

By default, xdebug will not work unless you add the query string XDEBUG_SESSION=PHPSTORM to every request made in the browser, or set it in the browser's cookies. Shouldn't there be web extensions that make this easier for me? Yes there should be, and yes there are! I know of extensions for Firefox. Simply Chrome and Safari press ⇧ ⌥ X to toggle the cookie in your session.

JSON

Making a SQL query and converting it into JSON isn't too rough.

<?php
// Generate a SQL query, submit it, and save the results
// -------------------------
// Print the SQL statement
$sql = "SELECT * FROM temporary_table;";
$results = $mysqli->query($sql);
// -------------------------
// Print the query results
echo "<samp class="output">
";
var_dump($results);
echo "
</samp>";
// -------------------------
// Return one result (row) as an associative array
while ($row = $results->fetch_object()) {
    $rows[] = $row;
}
?>
<!DOCTYPE html>
<html>
<head>
    <script>
        let rows = <?=json_encode($rows);?>
        console.log(rows)
    </script>
</head>
<body>
</body>

HTTP Requests

An example of getting a GET request from a form whose action is form.php and method is GET

<?php

// This is an associative array
var_dump($_GET);

var_dump($_GET["email"]);

?>

For a PHP-hosted server, files above the document root means they cannot be accessed publicly by a URL or Web Address (unless you create a script that specifically serves them).

MAMP

I learned PHP taking USC's course "ITP 303: Full-Stack Development", where we were taught to use MAMP. JetBrains wrote a PhpStorm MAMP tutorial

Composer

I've been told that Composer is a useful tool for managing PHP dependencies. You can download it on macOS with the following command:

brew install composer