Note: This content applies only to Cloud Functions (1st gen). See Cloud Functions version comparison for more information. Show
This guide takes you through the process of writing a Cloud Function using the PHP runtime. There are two types of Cloud Functions:
The sample shows how to create a simple HTTP function. Learn more: For more details, read about and CloudEvent functions.Guide structureCreating a GCP project using gcloud CLI
Creating a function
Specifying dependenciesYou use Composer to manage dependencies in PHP. If you don't already have Composer installed, you can do so as follows:
Next, specify your function's dependencies:
Building and testing locallyOnce you have completed the steps in , you can build and test your function locally. The following command creates a local web server running your 6 function:
If the function builds successfully, it displays a URL. You can visit this URL with your web browser: 1. You should see a 2 message.Alternatively, you can send requests to this function using 3 from another terminal window: Note: See the Cloud Functions testing documentation for information on writing unit tests for HTTP functions and CloudEvent functions.Deploying the functionTo deploy the function with an HTTP trigger, run the following command in the 7 directory:
The 5 flag lets you reach the function . To require , omit the flag.Learn more: For more details, read about deploying Cloud Functions.Testing the deployed function
Viewing logsUsing the command-line toolLogs for Cloud Functions are viewable in the Cloud Logging UI, and via the Google Cloud CLI. To view logs for your function with the gcloud CLI, use the 8 command, followed by the name of the function: 3The output should resemble the following: 4Note: There is typically a slight delay between when log entries are created and when they show up in Cloud Logging.
What is a function in PHP?PHP User Defined Functions
A function is a block of statements that can be used repeatedly in a program. A function will not execute automatically when a page loads. A function will be executed by a call to the function.
What is PHP function example?A function is a piece of code that takes another input in the form of a parameter, processes it, and then returns a value. A PHP Function feature is a piece of code that can be used over and over again and accepts argument lists as input, and returns a value. PHP comes with thousands of built-in features.
How to declare a function in PHP?Creating a Function
To call a function we just need to write its name followed by the parenthesis. A function name cannot start with a number. It can start with an alphabet or underscore. A function name is not case-sensitive.
What are the types of functions in PHP?Types of Functions in PHP. There are two types of functions as: Internal (built-in) Functions. User Defined Functions.
|