<?php function square($num){ return $num * $num; } echo square(4); //outputs'16'. ?>以上程式碼的執行結果為:
16
<?php function small_numbers(){ return array(0, 1, 2); } list($zero, $one, $two) = small_numbers(); echo $zero . $one . $two; ?>程式碼執行結果為:
012
$zero $one $two 的值分別是0、1、2。<?php function sum($a, $b):float{ return $a + $b; } var_dump( sum(1,2) ); ?>以上程式會輸出:
float(3)
<?php declare(strict_types=1); function sum($a, $b):int{ return $a + $b; } var_dump( sum(1,2) ); var_dump( sum(1,2.1) ); ?>以上程式的執行結果為:
int(3)
Fatal error: Uncaught TypeError: Return value of sum() must be of the type integer, float returned in /Library/WebServer/Documents/book/str.php:281 Stack trace: #0 /Library/WebServer/Documents/book/str.php(284): sum(1, 2.1) #1 {main} thrown in /Library/WebServer/Documents/book/str.php on line 281