Reputation: 11
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);
}
}
?>
Upvotes: 0
Views: 93
Reputation: 168755
I'd recommend using the built-in PHP function array_sum()
rather than rolling your own sum()
function.
Lets say you have a set of numbers you want to work with. If you put them into an array, you can then use array_sum()
to add them up, and count()
to find out how many numbers there are.
<?php
$data = array(1,2,3,4);
echo "<br/> Sum function:".array_sum($data)."<br/>";
echo "Average function:".(array_sum($data) / count($data))."<br/>";
?>
If you insist on writing your own mean()
function in the style of your existing sum()
function, then you've already answered your own question by using the func_num_args()
, which does exactly what you're asking for, and tells you how many arguments were passed into the function.
When it comes to your second question, I'll refrain for giving a direct answer, as the whole topic of profanity filters has been beaten to death on this site. It's generally considered to be an impossible problem to solve, as it's always easy to find ways around any filter, and they generally throw up false positives at the most unfortunate moments (even if your function worked, it would fall foul of the Clubttic mistake . But if you insist on writing one, I'll direct you to this question here on SO, which has a good answer for PHP: How do I replace bad words with php?
Hope that helps.
Upvotes: 0
Reputation: 73011
No need to reinvent the wheel. Take advantage of PHP native functions, like array_sum()
in combination with your func_*
functions.
function sum() {
return array_sum(func_get_args());
}
function avg() {
$args = func_num_args();
if ($args == 0) {
return 0;
}
return array_sum(func_get_args()) / $args;
}
As far as your phptest()
function, see the comment by Glass Robot.
Upvotes: 2