The PHP runtime

Your Cloud Run function runs in an environment consisting of an operating system version with add-on packages, language support, and the PHP Functions Framework library that supports and invokes your function. This environment is identified by the language version, and is known as the runtime ID.

Function preparation

You can prepare a function directly from the Google Cloud console or write it on your local machine and upload it. To prepare your local machine for PHP development, see Use PHP on Google Cloud.

Select your runtime

Cloud Run functions supports several versions of PHP, listed on the Supported language runtimes and base images. You can select the preferred PHP runtime for your function during deployment.

gcloud

To use Cloud Run functions to deploy an HTTP function using the gcloud CLI, see Deploy using the Google Cloud CLI.

Console

If you're using the Google Cloud console, select the runtime when you create and deploy your function. See the Google Cloud console quickstart for detailed instructions.

Source code structure

For Cloud Run functions to find your function's definition, your source code must follow a specific structure. See Write Cloud Run functions for more information.

PHP Configuration

You configure your PHP function with a php.ini file in your function's root directory. You can view existing PHP configuration settings with the phpinfo() function as shown in the following code sample:


use Psr\Http\Message\ServerRequestInterface;

function phpInfoDemo(ServerRequestInterface $request): string
{
    // phpinfo() displays its output directly in the function's
    // HTTP response, so we don't need to explicitly return it
    //
    // Note: we recommend deleting the deployed Cloud Function once you no
    // longer need it, as phpinfo() may broadcast potential security issues.
    phpinfo();
    return '';
}

Specifying dependencies

PHP uses Composer to manage dependencies. You specify dependencies for your function by adding the dependencies to a project file called composer.json.

The Cloud Run functions PHP runtime requires the Functions Framework to be an explicit dependency. To add Functions Framework as a dependency, run the following command in the directory containing your function code (this directory must also contain the composer.json file):

composer require google/cloud-functions-framework

This adds the Functions Framework to your composer.json and installs the package in the vendor/ directory.

autoload.php file

One of the files contained in your vendor/ directory is autoload.php.

Add the following line to the top of your PHP scripts to require the autoload.php file, which automatically requires your function's other dependencies:

require_once __DIR__ . '/vendor/autoload.php';

By default, the vendor/ directory is ignored in the generated .gcloudignore file to reduce the number of files sent in deployment.