php函數(shù)可以返回各種內(nèi)置數(shù)據(jù)類型,包括:1. 整數(shù);2. 浮點(diǎn)數(shù);3. 字符串;4. 布爾值;5. 數(shù)組;6. 對(duì)象;7. null。
PHP 函數(shù)返回哪些內(nèi)置數(shù)據(jù)類型?
PHP 函數(shù)可以返回各種內(nèi)置數(shù)據(jù)類型,包括:
整數(shù) (int)
浮點(diǎn)數(shù) (float)
字符串 (string)
布爾值 (bool)
數(shù)組 (array)
對(duì)象 (object)
NULL
實(shí)戰(zhàn)案例
假設(shè)我們有一個(gè)函數(shù) calculate_age(DOB)
來(lái)計(jì)算基于給定出生日期的年齡。此函數(shù)返回一個(gè)整數(shù)類型的年齡。
function calculate_age(string $DOB): int { $dobDate = new DateTime($DOB); $todayDate = new DateTime('today'); $age = $todayDate->diff($dobDate)->y; return $age; }
登錄后復(fù)制
調(diào)用此函數(shù):
$age = calculate_age('1980-01-01'); echo "Age: $age"; // 輸出:Age: 43
登錄后復(fù)制
其他示例
function is_valid(string $email): bool { return filter_var($email, FILTER_VALIDATE_EMAIL); }
登錄后復(fù)制
function get_products(): array { // 從數(shù)據(jù)庫(kù)獲取產(chǎn)品列表 $productDetails = [ ['id' => 1, 'name' => 'Product 1'], ['id' => 2, 'name' => 'Product 2'] ]; return $productDetails; }
登錄后復(fù)制