php中一個方法只有一個返回值。php方法中,返回值使用return語句定義,語法“return 返回值;”。return語句只能返回一個參數(shù),即方法(函數(shù))只能有一個返回值;如果想要返回多個值,就需在方法(函數(shù))中定義一個數(shù)組,將返回值存儲在數(shù)組中進行返回。
本教程操作環(huán)境:windows7系統(tǒng)、PHP8.1版、DELL G3電腦
php中一個方法只有一個返回值。
PHP 中提供了 return 語句來返回方法(函數(shù))的運行結(jié)果,其語法格式如下:
return 返回值;
注:“返回值”與 return 關(guān)鍵字之間需要使用空格分隔。
可以看出,return 語句只能返回一個參數(shù),即只能返回一個值,不能一次返回多個值。
<?php header("Content-type:text/html;charset=utf-8"); class Website { public function demo($num) { return $num * $num; } } $student = new Website(); echo $student -> demo(4); ?>
如果要返回多個值的話,就需要在函數(shù)中定義一個數(shù)組,將返回值存儲在數(shù)組中返回。
<?php header("Content-type:text/html;charset=utf-8"); class Website { public function demo() { return array(0, 1, 2); } } $student = new Website(); var_dump($student -> demo(4)) ; ?>
推薦學習:《PHP視頻教程》