tencent cloud

All product documents
Serverless Cloud Function
Examples
Last updated: 2024-12-02 18:12:22
Examples
Last updated: 2024-12-02 18:12:22
These examples provide code snippets based on PHP 7.2 for your reference.
You can click scf-php-code-snippet to obtain the relevant code snippets and directly use them for deployment.

Obtaining Environment Variables

This example shows you how to obtain all or a single environment variable.
<?php
function main_handler($event, $context) {
print_r($_ENV);
echo getenv('SCF_RUNTIME');
return "hello world";
}
?>

Formatting Local Time

This example shows you how to output date and time in the specified format.
The SCF environment uses the UTC format by default. To output in Beijing time, you can add the TZ=Asia/Shanghai environment variable to the function, and use date_default_timezone_set(getenv('TZ')); in the function code to set the needed time zone, as shown below:
<?php
function main_handler($event, $context) {
date_default_timezone_set(getenv('TZ'));
echo date("Y-m-d H:i:s",time());
return "hello world";
}
?>

Initiating Network Connections in a Function

<?php
function main_handler($event, $context) {
$url = 'https://cloud.tencent.com';
echo file_get_contents($url);
return "hello world";
}
?>
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support