★ wanayoo — archive 1999 http://fr.php.net/manual/fr/function.array-sum.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
<array_splicearray_unique>
view the version of this page
Last updated: Wed, 15 Jan 2003

array_sum

(PHP 4 >= 4.0.4)

array_sum --  Calcule la somme des valeurs du tableau

Description

mixed array_sum ( array arr)

array_sum() retourne la somme des valeurs du tableau, sous forme d'un entier ou d'un nombre à virgule flottante.

Exemple 1. Exemple avec array_sum()

<?php
  $a = array(2,4,6,8);
  echo "somme(a) = ".array_sum($a)."\n";
// affiche : somme(a) = 20
  $b = array("a"=>1.2,"b"=>2.3,"c"=>3.4);
  echo "somme(b) = ".array_sum($b)."\n";
// affiche : somme(b) = 6.9
?>

User Contributed Notes
array_sum
add a note about notes
mlong at mlong dot org
05-Apr-2002 12:08

Quoting:
Note: PHP versions prior to 4.0.6 modified the passed array itself and converted strings to numbers (which most of the time converted them to zero, depending on their value).
--

FYI This is still the behaviour in 4.1.1.

n dot dose at leo-media dot de
09-Apr-2002 11:33

> Note: PHP versions prior to 4.0.6 ...

And also on 4.1.2 (on windows).

office at at at universalmetropolis dot com
17-Mar-2003 04:22

A practical implementation of this function: creating a standardised error-checking process.

// check some fields

if(strlen(trim($username))<3){

$errormessage[]="You need to enter a username";

}else{

$errormessage[]=0;

}

if(strlen(trim($password))<3){

$errormessage[]="You need to enter a password";

}else{

$errormessage[]=0;

}

// check to see if there were any errors (an error-free submission will have a sum of 0)

if(array_sum($errormessage)!=0){

print "error!"; // (I use a separate error display function)

}else{

// no errors found, proceed with submission

}

add a note about notes

<array_splicearray_unique>
 Last updated: Wed, 15 Jan 2003
show source | credits | stats | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Mon Mar 24 04:11:16 2003 CET