function multiply (...$nums ){
$result = 1;
foreach ($nums as $num ){
if (gettype($num)==gettype("k")){
continue;
}
echo $result = $result*$num;
}
}
echo multiply(10, 20);
echo "<br>";
echo multiply("A", 10, 30);
echo "<br>";
echo multiply(100.5, 10, "B");
I tried to make a function to multiply the argument that is given to it, but if the argument is a string, it skips it, and if the argument is a float, it converts it to an int before starting the multiplication process
2
Answers
Try this:
is_string
you can use to check if it is a string and then if it is skip it.is_float
to check if it is float and if it is useintval
to convert…You could use
array_filter()
to remove not-numeric values,array_map()
to cast into integers, and then,array_product()
to compute the product of these values.Code (demo)
Output: