Ds \ Vector::sum()函数是PHP中的内置函数,它返回向量的所有元素的总和。
用法:
number public Ds\Vector::sum( void )
参数:该函数不接受任何参数。
返回值:此函数返回向量中所有元素的总和。取决于矢量元素,总和可以为int或float类型。
以下示例程序旨在说明PHP中的Ds \ Vector::sum()函数:
程序1:
<?php
// Declare the new Vector
$arr = new \Ds\Vector([3, 6, 1, 2, 9, 7]);
echo("Original vector:\n");
// Display the vector elements
var_dump($arr);
echo("\nSum of elements:\n");
// Use sum() function to returns the
// sum of all vector elements
var_dump($arr->sum());
?>输出:
Original vector:
object(Ds\Vector)#1 (6) {
[0]=>
int(3)
[1]=>
int(6)
[2]=>
int(1)
[3]=>
int(2)
[4]=>
int(9)
[5]=>
int(7)
}
Sum of elements:
int(28)
程序2:
<?php
// Declare the new Vector
$arr = new \Ds\Vector([3.5, 6, 1, 2.7, 9, 7.3]);
echo("Original vector:\n");
// Display the vector elements
print_r($arr);
echo("\nSum of elements:");
// Use sum() function to returns the
// sum of all vector elements
print_r($arr->sum());
?>输出:
Original vector:
Ds\Vector Object
(
[0] => 3.5
[1] => 6
[2] => 1
[3] => 2.7
[4] => 9
[5] => 7.3
)
Sum of elements:29.5
参考: http://php.net/manual/en/ds-vector.sum.php
相关用法
- p5.js str()用法及代码示例
- p5.js min()用法及代码示例
- PHP end()用法及代码示例
- PHP sin( )用法及代码示例
- PHP cos( )用法及代码示例
- p5.js hue()用法及代码示例
- PHP pos()用法及代码示例
- p5.js hex()用法及代码示例
- p5.js red()用法及代码示例
- p5.js tan()用法及代码示例
- p5.js log()用法及代码示例
- p5.js sin()用法及代码示例
注:本文由纯净天空筛选整理自barykrg大神的英文原创作品 PHP | Ds\Vector sum() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
