note 59262 deleted from function.count by googleguy

From: Date: Mon, 17 Sep 2012 00:16:38 +0000
Subject: note 59262 deleted from function.count by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190837@lists.php.net to get a copy of this message
Note Submitter: Tom ---- 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... } } ?>

« previous php.notes (#190837) next »