从 PHP 的函数返回两个值
两个变量无法显式返回,它们可以放入列表/数组数据结构中并返回。
示例
function factors( $n ) {
// An empty array is declared
$fact = array();
// Loop through it
for ( $i = 1; $i < $n; $i++) {
// Check if i is the factor of
// n, push into array
if( $n % $i == 0 )
array_push( $fact, $i );
}
// Return array
return $fact;
}
// Declare a variable and initialize it
$num = 12;
// Function call
$nFactors = factors($num);
// Display the result
echo 'Factors of ' . $num . ' are: <br>';
foreach( $nFactors as $x ) {
echo $x . "<br>";
}输出
这将生成以下输出 −
Factors of 12 are: 1 2 3 4 6
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
JavaScript
PHP