Question about calculating Average in PHP [duplicate] - php

This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.

Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;

You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);

Related

How to increase number in php if starting value is 0 [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 2 years ago.
I am getting issue when increasing number.
For example:
$r = 000123 //Integer
Now i want
$r = 000124
I have try with multiple way.. but i didn't get the result. because of starting with 000.
You can do it this way
$num = "00000123";
// get number without zero prefix
preg_match('/(?!0+)\d+/', $num, $match);
// increase by 1
$match[0]++;
// add zero prefix
$newNum = sprintf('%08d', $match[0]);
var_dump($newNum);
What if you try:
$r = 000123
$r++

Ho do I output integers with leading zeros in PHP? [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 3 years ago.
I need a little help with some PHP code. I looked at the examples for doing this with JavaScript and used that as the basis for a PHP implementation. Two issues I haven't be able to resolve. I still get a leading 1 at the start of the string. I also get fewer zeros than specified once the values have trailing zeros (i.e 10, 20, etc).
$num = 2;
$numZeros = 5;
function listRiskNumber($num, $numZeros) {
$n = abs($num);
$zeros = max(0, $numZeros - strlen(floor(json_encode($n))));
$zeroString = substr((pow(10,$zeros)),0,5);
if( $num < 0 ) {
$zeroString = '-' + $zeroString;
}
return $zeroString + $n;
}
$row = listRiskNumber($num, $numZeros);
echo $row;
I want to turn the leading 1 into a zero, and ensure trailing zeros don't get cut off. Any help would be greatly appreciated.
sprintf("%08d", $value);
Solved the problem. Thanks all for the quick answers.

How to show only two numbers after decimal using php eval() or round()? [duplicate]

This question already has answers here:
How can I format a number into a currency value without php?
(3 answers)
Closed 4 years ago.
I am calculating result of my expression using eval() function but its showing all the numbers afer decimal and I want to show only two numbers after decimal
this is my eval function with other data that I have stored.
$expression = generate_expression($num_operands,
$operations, $num_digits);
$expression_string = implode(" ", $expression);
$result = eval("return ($expression_string);");
$expressions[] = compact("expression", "result");
I am storing the expression_
for this I tried using round function also but its not showing the result that I want
here is the round function for the $result
<?php echo round($result,2); ?>
Where I went wrong?
Use: number_format()
<?php echo number_format((float)$result, 2); ?>
Example

variable changes when using if statement [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
I've just started learning php an hour ago. I've made this code:
$x=2;
$y=4;
echo $x;
echo $y;
if($x=5)
{
echo "$x";
}
else
{
echo "test";
}
I'm expecting the output: 24test
I'm getting the output: 245
x equals 2 in the beginning. Why is x then changing to 5 when the only thing I do is CHECKING whether x = 5?
I've searched the web and this site for an answer but couldn't find anything. Thanks in advance!
Tony
Note that = is use to assign value while == use to evaluate value within if statement so your code should be
if($x==5) {
Instead of
if($x=5) {

Need one digit after decimal without changing any number [duplicate]

This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 8 years ago.
I have a value 3.9609053497942. I need value 3.9 means only one value after decimal. I have used PHP number_format and round functions but it is giving me answer 4.
You could multiply the number by 10, floor() it, and then divide it back.
echo floor($value * 10) / 10;
Try with this,
echo intval((3.9609053497942*10))/10;
or
echo floor((3.9609053497942*10))/10;
There is so many possible solutions:
echo bcadd(3.9609053497942, 0, 1);
preg_match('/\d*\.\d/', 3.9609053497942, $matches);
echo $matches[0];
why not treat it as a string, like
$x = (string)3.96;
$y = explode(".",$x);
$result = $y[0] . "." . $y[1];
Did you tried like:
number_format(3.9609053497942, 1);

Categories