The PHP function format is generally as follows:
<?php
function main_handler($event, $context) {
print_r ($event);
print_r ($context);
return "hello world";
}
?>
You need to specify the execution method when creating a SCF function. If the PHP programming language is used, the execution method is similar to index.main_handler
, where index
indicates that the executed entry file is index.php
, and main_handler
indicates that the executed entry function is main_handler
.
When submitting the ZIP code package by uploading the ZIP file locally or through COS, make sure that the root directory of the ZIP package contains the specified entry file, which contains the entry function specified by the definition, file name, function name, and execution method; otherwise, execution will fail as the entry file or entry function cannot be found.
The input parameters in the PHP environment include $event
and $context
.
The event parameter varies with trigger or event source. For more information on its data structure, see Trigger Overview.
Note:The input parameters
event
andcontext
in PHP 8.0, 7.4, 7.2, and 5.6 are inobject
format.
Your handler can use return
to return a value. The return value will be handled differently depending on the type of invocation when the function is invoked.
In the PHP environment, a serializable object such as dict
object can be directly returned:
Note:
- The return value of both sync and async invocations will be recorded in the function logs. The return value will be written to the function invocation log
SCF_Message
in the format ofResponse RequestId:xxx RetMsg:xxx
.- The value of
SCF_Message
is limited to 8 KB in length, and excessive parts will be truncated.
You can exit the function by calling die()
. At this point, the function will be marked as execution failed, and the output from the exit using die()
will also be recorded in the log.
Was this page helpful?