📗
Everyday Cheat Sheets
  • README
  • Build
    • Architecture
      • API Management
        • Kong
      • Authentication
        • Keycloak
        • Okta
      • Cloud Native
      • Design Patterns
      • Design Principles
      • IaC
      • IoT
      • Message Broker
      • Methods
      • Networking
      • Payment
        • Stripe
      • Testing
    • Code lifecycle (ALM)
      • Automation Pipelines
        • Argo CD
        • CircleCI
        • Flux
        • Keptn
        • Travis
      • Azure DevOps
        • Azure Pipelines
      • Chef
      • GitHub
        • GitHub Actions
      • Nexus
      • Promyze
      • RunDeck
      • SaltStack
      • Sonar
      • Tuleap
    • Containers & Cloud Native
      • Argo Workflows
      • Containerization
        • Docker
          • Docker CLI
        • containerd
        • cri-o
      • CNAB
      • Dapr
      • Envoy
      • Fluentd
      • Knative
      • Kubernetes
        • Cluster API
        • etcd
        • Helm
        • k3d
        • kind
        • Kubectl
        • MetalLB
        • Minikube
      • Open Application Model (OAM)
      • Unleash
    • Data storage
      • MySQL
      • MongoDB
        • Atlas
        • Compass
        • Evergreen
        • MongoDB 4.2
        • MongoDB 5.0
        • MongoDB design
        • MongoDB events
        • MongoDB driver for .NET
        • Ops Manager
        • Realm
      • Oracle
      • Redis
      • SQL Server
      • PostgreSQL
    • Frameworks & libraries
      • Angular
        • Angular CLI
        • Angular events
      • .NET
        • ASP.NET Core
        • Blazor
        • .NET 5.0
        • .NET 6.0
        • .NET CLI
        • .NET Core
        • .NET Events
        • .NET Logging
        • .NET Testing
        • NuGet
        • WPF
        • Xamarin
      • gRPC
      • Ionic
      • Jekyll
      • Node.js
        • Express
        • NPM
      • React
        • React Native
      • Redux
    • IDE
      • Visual Studio 2022
    • Languages
      • C#
        • C# 8.0
      • ECMAScript
      • GraphQL
      • JavaScript
        • webpack
        • Yarn
      • MS-DOS
      • PHP
      • PowerShell
      • Python
      • Swagger
      • TypeScript
    • Messaging
      • Azure Service Bus
      • RabbitMQ
    • Testing
    • Workstation
      • QGIS
      • Visual Studio 2019
      • Windows 10
      • Windows Subsystem for Linux
  • Collaborate
    • Marp
    • Microsoft 365
      • Microsoft Graph
      • SharePoint Framework
        • Fluent UI
        • SharePoint Framework UI components
  • Run
    • Cloud computing
      • Alibaba
      • AWS
      • Azure
        • Azure AD
        • Azure CLI
        • Azure Container Registry
        • Azure Portal
        • Azure Service Bus
      • Firebase
      • OVH
    • Hardware
      • Single-board computers
        • Odroid
        • Raspberry Pi
    • Infrastructure automation
      • Azure Resource Manager
      • Packer
      • Pulumi
      • Puppet
      • Terraform
        • HCL
        • Terraform CLI
        • Terraform Providers
    • Networking
      • HAProxy
      • nginx
    • Observability
      • Grafana Labs
        • Grafana
        • Loki
        • Tempo
      • OpenTelemetry
      • Prometheus
      • Splunk
    • Security
      • Falco
    • Systems
      • Linux
        • CentOS
        • eBPF
        • Linux Kernel
        • Rocky
        • Ubuntu
      • Windows Server
    • Virtualization
      • Hyper-V
      • Vagrant
  • Optimize
    • DevOps
  • Join
    • Companies
      • HashiCorp
Powered by GitBook
On this page
  • Dev setup
  • PHP binaries
  • Composer
  • XDebug
  • IDE
  • Language
  • Conventions
  • MongoDB Driver
  • Libraries
  • Tools
  • phpdbg
  • Unit tests
  • PHPUnit

Was this helpful?

Export as PDF
  1. Build
  2. Languages

PHP

PreviousMS-DOSNextPowerShell

Last updated 1 year ago

Was this helpful?

→

Dev setup

PHP binaries

Download the version that you want from the . You may consider reading a little about thread safe (for Apache) and not thread safe versions (for IIS, nginx or command line).

On Windows, you'll download a zip file, you just have to extract it somewhere (more in a program folder). You can add the direetory in the environment PATH but it's not mandatory. For example, you can add the following line in a command window:

# Windows
SET PATH="%PATH%;D:\Programs\php-7.2.2-nts-Win32-VC15-x64"

Once you're clear on your PHP installation setup, you should be able to get something like this:

php -v
# PHP 7.2.2 (cli) (built: Jan 31 2018 19:31:15) ( NTS MSVC15 (Visual C++ 2017) x64 )
# Copyright (c) 1997-2018 The PHP Group
# Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

By default, there is no php.ini file in the installation folder, you can copy the development one to create your own file.

Nginx

CGI

  • Create a bat file, for example start-php-fcgi.bat:

@ECHO OFF
ECHO Starting PHP FastCGI...
set PATH=D:\Programs\php-7.2.4-nts-Win32-VC15-x64;%PATH%
c:\bin\RunHiddenConsole.exe D:\Programs\php-7.2.4-nts-Win32-VC15-x64\php-cgi.exe -b 127.0.0.1:9123

Composer

Once it is installed, you should be able to get the following:

composer -V
# Composer version 1.6.3 2018-01-31 16:28:17

You can install packages system or project wide. Here are some tools that are good to have system wide:

# this will install phpcs
composer global require squizlabs/php_codesniffer
# (optional) add standard rules by default
phpcs --config-set default_standard Squiz

Composer configuration file is called composer.json.

XDebug

XDebug is required to debug your code in your IDE (Eclipse PDT, VS Code, etc.).

  • Copy the file in the ext directory of your PHP installation directory.

  • Edit your php.ini file to add the following lines to activate the debugging (if you don't know which php.ini file you're using make a phpinfo() file)

# after all the ;extension lines in [PHP] section at the beginning of the file
# For Windows
zend_extension=D:/Programs/php-7.2.2-nts-Win32-VC15-x64/ext/php_xdebug-2.6.0-7.2-vc15-nts-x86_64.dll

# at the end of the file
[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

IDE

VS Code

You just have to set php.validate.executablePath in your user settings in VS Code if it's not in the PATH (which could be a good practice if several PHP versions must be present on the machine).

Language

Conventions

phpcs is a good tool to validate the code that is written.

You can also change the behaviour directly in the file.

// phpcs:disable
// phpcs:enable
// phpcs:ignoreFile

For example, custom_ruleset.xml file content:

<?xml version="1.0"?>
<ruleset name="Custom Standard">
    <rule ref="Squiz">
        <exclude name="Squiz.Commenting.FileComment.Missing"/>
        <exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar" />
    </rule>
</ruleset>

You can configure it in VS Code or from the command line:

phpcs my/path/to/file.php --standard=my/path/to/custom_ruleset.xml

MongoDB Driver

Installation

  • Add in your php.ini file:

# For Windows
extension=php_mongodb.dll
  • From your project directory:

composer require mongodb/mongodb

First steps

<?php

require 'vendor/autoload.php';

$client     = new MongoDB\Client('mongodb://localhost:27017');
$collection = $client->demo->beers;

$result = $collection->insertOne([ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ]);

echo "Inserted with Object ID '{$result->getInsertedId()}'\n";

$result = $collection->find([ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ]);

foreach ($result as $entry) {
    echo $entry['_id'], ': ', $entry['name'], "\n";
}

Libraries

Tools

phpdbg

Unit tests

PHPUnit

Getting started with PHPUnit

  • Install locally: composer require phpunit/phpunit --dev

    • Check the tool is running file: php vendor/phpunit/phpunit/phpunit --version (or php vendor/bin/phpunit on a non-Windows environment)

  • Edit composer.json file and execute composer install to regenerate autoload file

    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    }
  • Create phpunit.xml.dist file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
    <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
             backupGlobals="false"
             colors="true"
             bootstrap="config/bootstrap.php">
        <php>
            <ini name="error_reporting" value="-1" />
            <env name="APP_ENV" value="test" />
            <env name="SHELL_VERBOSITY" value="-1" />
        </php>
        <testsuites>
            <testsuite name="Project Test Suite">
                <directory>tests</directory>
            </testsuite>
        </testsuites>
        <filter>
            <whitelist>
                <directory>src</directory>
            </whitelist>
        </filter>
    </phpunit>
  • Launch the tests: php vendor/phpunit/phpunit/phpunit -c phpunit.xml.dist --log-junit junit.xml

  • You can also use Symfony PHPUnit bridge: composer install symfony/phpunit-bridge --dev (and create a bin/console and update your gitignore)

is the dependency (package) manager for PHP.

Go on and select the version related to your PHP one, for example php_xdebug-2.6.0-7.2-vc15-nts-x86_64.dll.

Visual Studio Code is an indredible tool provided by Microsoft. You can review for more information.

If you want to see a preview or get up to speed quickly, you can watch .

It can be easily configured, see the .

You can define your own ruleset, see .

There is a PHP extension and a PHP library to access MongoDB, see .

(Windows) Download the zip file for the good version from and copy the dll inside the zip file to your extension folder.

Reference: .

Reference: .

: A simple PHP Dependency Injection Container

is a testing framework for PHP. It is open source: . It is available on .

Read

With coverage (): php vendor/phpunit/phpunit/phpunit -c phpunit.xml.dist --coverage-html .build/coverage-report --coverage-clover .build/coverage.xml

Integration in SonarCloud:

We can also use phpdbg (that is now shipped with PHP): phpdbg -qrr vendor/phpunit/phpunit/phpunit -c phpunit.xml.dist (comparison with xdebug here: )

For Mocks :

php.net
download page
Composer
Installation page
PHP Programming in VS Code
laracasts.com series
Configuration Options
Annotated ruleset.xml
docs.mongodb.com
pecl.php.net
php.net
Using the PHP Library for MongoDB (PHPLIB)
Pimple
PHP RFC: phpdbg
krakjoe/phpdbg
PHPUnit
sebastianbergmann/phpunit
packagist
Getting Started with PHPUnit 8
Code Coverage Analysis
PHP Unit Tests and Coverage Results Import
Generating Code Coverage with PHPUnit and phpdbg
Test Doubles