Can someone fix my functions?
First, how can I go about getting the mean from my numbers that are used in the sum function? What varaible can I use to divide the numbers if I don't know how many numbers will be used?
Second, why isn't my find replace function working? When I run the string with the 'bad' words, i get this (i just entered ugly):
array(1) { [0]=> string(4) "nice" }
I want the whole string returned with just the bad words replaced...
also, even tho its in there its not returning the length of the string and the number of words in the string! Grr...Pretty frustrated so i would be very grateful for any help...
File one: (to get the return of the functions)
<?php
require('myfunctions.php');
if($_POST) {
$result = phptest($_POST['input']);
if ($result === false) {
echo 'No rude words were found.';
} else {
var_dump($result);
}
}
?>
<?php
echo "<br/> Sum function:".sum(1,2,3,4)."<br/>";
echo "Average function:".mean(1,2,3,4)."<br/>";
?>
<form action="" method="post">
<input name="input" type="text" size="20" maxlength="20" />
<input name="submit" type="submit" value="submit" />
</form>
myfunctions:
<?php
function sum()
{
$sum = 0;
for ($i=0; $i<func_num_args(); $i++) $sum+= func_get_arg($i);
return $sum;
}
?>
<?php
function phptest($input) {
$search = array('ugly', 'rude');
$replace = array('nice', 'sweet');
$output = str_ireplace($search, $replace, $input, $replace_count);
if ($replace_count === 0) {
return false;
} else {
return explode(' ', $output);
}
}
?>