Skip to content

PHP

November 2, 2023
May 28, 2021

PHP: Hypertext Preprocessor
PHP - ArchWiki
ziadoz/awesome-php: A curated list of amazingly awesome PHP libraries, resources and shiny things.

The Definitive PHP 7.2, 7.3, 7.4, 8.0, and 8.1 Benchmarks (2023)
Modern tools for PHP developers - LogRocket Blog > 7.0 in 2015, > 8.1 in 2022
security-checklist/php-security-check-list: PHP Security Check List [ EN ] 🌋 ☣️
Upgrading your site to PHP 8 - The Joomla Community Magazine Frameworks takes time to port to latest PHP, try upgrading in May-Aug for the Dec PHP release

Create a PHP REST API : Write a RESTful API from Scratch using Plain, Object-Oriented PHP and MySQL - YouTube

Commentaries

Is PHP Dead? No! At Least Not According to PHP Usage Statistics
PHP in decline: The rise and fall of a programming language - JAXenter

Does PHP Suck?! - The Answer May Surprise You! - YouTube
Does PHP Suck? | Prime React - YouTube

PHP doesn't suck (anymore) - YouTube
PHP Doesn't Suck Anymore? | Prime Reacts - YouTube

the bad

Is PHP a badly designed programming language? - Quora
Why is PHP hated by so many developers? - Quora
PHP: a fractal of bad design / fuzzy notepad
PHP Sadness
The PHP Singularity

Installation

#Docker PHP FPM

AUR (en) - php81

yay -S php81-cli php81-phar

Config

PHP: Description of core php.ini directives - Manual

$ php --ini
Configuration File (php.ini) Path: /etc/php7
Loaded Configuration File:         /etc/php7/php.ini
Scan for additional .ini files in: /etc/php7/conf.d
Additional .ini files parsed:      (none)
php -m  # show modules

echo '<?php phpinfo(); ?>' > /srv/http/phpinfo.php
# visit http://localhost/phpinfo.php

cat << 'EOF' > version.php
<?php
// prints e.g. 'Current PHP version: 4.1.1'
echo 'Current PHP version: ' . phpversion();

// prints e.g. '2.0' or nothing if the extension isn't enabled
echo phpversion('tidy');
?>
EOF

Version Manager

phpbrew

phpbrew/phpbrew: Brew & manage PHP versions in pure PHP at HOME

Dev Environment

Homestead

Laravel Homestead - Laravel - The PHP Framework For Web Artisans

Docker Homestead/Laradock

用 Docker 取代 Laravel Homestead 開發環境 | Laravel | Laravel China 社区
laraedit/laraedit-docker: Dockerized version of Laravel Homestead PHP7

shincoder/homestead - Docker Image | Docker Hub PHP7.3
jaouadk/homestead-docker: Multi app development container.

Laradock
laradock/laradock: Full PHP development environment for Docker.

Docker PHP FPM

PHPDocker.io - Generator !important, project creator
phpdockerio/php - Docker Image | Docker Hub
phpdocker-io/base-images: Base docker images for PHPDocker.io

richarvey/nginx-php-fpm: Nginx and php-fpm for dockerhub builds PHP 8

jniltinho/caddy-php-fpm: Caddy v2 + PHP-FPM 7.2.x + Composer built on Ubuntu
Yavin/docker-alpine-php-fpm: Docker image for php-fpm based on alpine linux that makes it small 7.2, with extensions

HHVM/Hack

HHVM | HHVM JIT VM for Hack, PHP 5 and majority of PHP 7.
The Hack Programming Language | Hack

Zend PHP 7.3 outperforms HHVM in PHP

PHP-FPM

Difference between PHP-CGI and PHP-FPM | BaseZap
PHP: FastCGI Process Manager (FPM) - Manual php-fpm
PHP: Configuration - Manual

PHP-FPM - HTTPD - Apache Software Foundation

Use cgi-fcgi for testing your PHP FPM endpoints.
inanzzz | Testing PHP-FPM without having a web server
Directly connect to PHP-FPM

Learn

PHP: PHP Manual - Manual
PHP: The Right Way !important
learning-zone/php-basics: PHP Basics ( v8.x )
PHP Best Practices: a short, practical guide for common and confusing PHP tasks

27 Best Tutorials to Learn PHP in 2021 (Free and Paid Resources)
8 Awesome and Free PHP Books - Tutorialzine
Learn Laravel | Laracasts also courses on other topics
PHP Tutorial for Beginners: Free 7-Hour Course

How to Learn PHP
PHP Tutorial => Getting started with PHP <= 7.1

Basic

The Basics - PHP: The Right Way

$ php -a
Interactive shell

php > echo 5+8;
13

php > $a = array(1, 2, array("a", "b", "c"));
php > var_dump($a);

php > echo phpinfo();
....
# php -S <ip:port> -t <root> <PHP file>
$ php -S 0.0.0.0:8080 -t public public/index.php

Builtin types

PHP: Types - Manual
PHP: Function Reference - Manual

Array

Array are actually map/dict of keys to values.
Key may be a string or an int, value can be any type.

PHP: Array Functions - Manual
array_column object.pick()
array_keys, array_values
array_combine zip()
compact lookup local variable and construct map

$array[] = 'ValueX'; // Append 'ValueX' to the end of the array
$array += ['keyX' => 'valueX', 'keyY' => 'valueY']; // adding/overwrite elements
$fruits = ['banana', 'apple'];
var_dump(in_array('banana', $fruits)); // true
var_dump(array_key_exists(2, $fruits)); // false
var_dump(array_search('apple', $fruits));  // return key or false
$colors = ["red", "green", "blue"]; // keys default to 0,1,2,...
foreach ($colors as $color) {
    echo "I am the color $color<br>";
}

$foods = ["healthy" => "Apples", "bad" => "Ice Cream"]; // explicit keys
foreach ($foods as $key => $food) {
    echo "Eating $food is $key";
}

Array in loop by value won't change during the loop, loop with reference with change will affect later iterations
Concurrency

foreach ($array as $key => $value) {
}

foreach ($array as $key => &$value) {
}

Class/Object

PHP: Classes/Objects - Manual

Variables

PHP: Variable handling - Manual
PHP: Variables From External Sources - Manual

PHP: $_SERVER - Manual
PHP: $_GET - Manual query param
PHP: $_POST - Manual support array, not dots (.)
PHP: $_REQUEST - Manual

How to receive JSON POST with PHP - GeeksforGeeks

// Takes raw data from the request
if ($_REQUEST["CONTENT_TYPE"] === "application/json") {
  $body = file_get_contents('php://input');

  // Converts it into a PHP object
  $data = json_decode($body);
}

Validation

works with strings

PHP: filter_var - Manual
PHP: filter_var_array - Manual
PHP: Types of filters - Manual

CLI

PHP: Command line usage - Manual

<?php
if ($argc !== 2) {
    echo "Usage: php hello.php <name>" . PHP_EOL;
    exit(1);
}
$name = $argv[1];
echo "Hello, $name" . PHP_EOL;

Linter/Style

PER Coding Style - PHP-FIG

Intelephense


Typst

Typst: Compose papers faster
Typst: The LaTeX alternative in Rust - YouTube