note 59262 added to function.count
| From: | Tom at osu1 dot php dot net | Date: | Thu, 01 Dec 2005 00:57:37 +0000 |
| Subject: | note 59262 added to function.count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99392@lists.php.net to get a copy of this message | ||
You can find an average from an array using this and array_sum.
<?php
//array average( array input )
function average($input) {
return array_sum($input) / count($input);
}
?>
You can also do a method of form validation that involves putting all errors into an array and
letting count() do the key part.
<?php
if(isset($_POST['submit'])) {
$errors = array();
if(empty($_POST['message'])) $errors[] = "Empty message field";
if(!preg_match('/[a-z0-9.]@[a-z0-9].[a-z]/i', $_POST['email']) {
$errors[] = "Bad email address";
}
if(count($errors) == 0) {
//process form...
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.count.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59262
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59262&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59262&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59262&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59262&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59262&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59262&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59262&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59262&report=yes
Search -- http://master.php.net/manage/user-notes.php