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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
