Home > Article > Backend Development > What are the specific types of PHP function return values?
PHP function return value types are divided into: 1. Basic data types (int, float, bool, string, NULL); 2. Special types (void, mixed); 3. Built-in and custom classes; 4. Composite data Type (array, object).
The specific type of the PHP function return value
In PHP, functions can be returned by using built-in type mapping or custom types. Specify the return type. The following lists the various specific types that PHP functions can return:
Basic data types:
Special type:
Built-in and custom classes:
Composite data type:
Practical case :
The following function gets the birthday from the user and returns a DateTime object:
function getBirthday(): DateTime { $dateString = readline('请输入您的生日(格式:yyyy-mm-dd):'); return new DateTime($dateString); }
In this example, the getBirthday()
function specifies that it will return a DateTime
object, this is a built-in class in PHP.
Use this function:
$birthday = getBirthday(); echo "您的生日是:{$birthday->format('Y-m-d')}" . PHP_EOL;
This will prompt the user for their birthday and format the returned DateTime
object into a readable string.
The above is the detailed content of What are the specific types of PHP function return values?. For more information, please follow other related articles on the PHP Chinese website!