if number is a decimal between 2 numbers (php) - php

I'm writings a PHP script that calculates an average of a number.
EXAMPLE:
$rating = 7;
$votes = 3;
$AvgRating = number_format($rating / $votes,1);
The return $AvgRating for this would be 2.3 (to 1 decimal place).
is it possible to say.....
if ($AvgRating > 2 && $AvgRating < 3){
$display = 'between';
}
echo $display;
I have tried but it does not work, I have tried google but don't know exactly what I need to look for.

This code evaluates correctly...
<?php
$rating = 7;
$votes = 3;
$AvgRating = number_format($rating / $votes,1);
if ($AvgRating > 2 && $AvgRating < 3){
$display = 'between';
}
echo $display;
?>

Hi there is some mistakes $AvgRating retunrs 2.3 not 7.3
So you have to check with this
$rating =7; $votes=3;
$AvgRating = number_format($rating / $votes,1);
if ($AvgRating > 2 && $AvgRating < 3.0){
echo $AvgRating;
}

If your question is whether the condition inside the if statement is correct, yes it is.
However in your case, the if condition fails since the value of $AvgRating (7/3) is less than 7. So you probably might be checking whether $AvgRating is greater than 2.

Related

How to make php generate random numbers untill the specific number is reached

I want a random integer to be generated in the range from 1 to 3 until 2 will be generated.
Please review the code below - What am I doing wrong?
Thank you!
<?php
$min = 1;
$max = 3;
$number = rand($min,$max);
while($number !== 2) {
echo ($number);
}
?>
your rand() is not in the while loop, so the rand() will execute one time.
If the $number is not 2, the while loop will execute without stoping.
If the $number is 2, the while loop will not executed.
while(($number = rand($min, $max)) != 2){echo $number;}

Is this expression a PHP bug? 6227020800 % 10 = 4

Writing some code connected with factorials (counting sum of numbers of factorial) I noticed that 13! modulus 10 equals 4.
function fact($n)
{
if ($n == 0) return 1;
return $n * fact($n-1);
}
function sum_num($n)
{
$sum = 0;
while ($n > 0)
{
$sum = $sum + ($n % 10);
$n = floor($n/10);
}
return $sum;
}
$n = 13;
$buff = fact($n);
echo $n."! = ".$buff;
echo "<br>";
echo "Summ ".$n."! = ".sum_num($buff);
Output is:
13! = 6227020800
Summ 13! = 31
But Summ should be 27. I begun search and found that on the first step I am getting 4 instead 0.
6227020800 % 10 = 4
And I don't understand why?
6227020800 requires 33 bits. The most siginficant bit gets truncated and you get you the number 1932053504 (modulo 10 of which is 4).
For arbitrary precision math use bc* functions, e.g. bcmod(6227020800, 10).
May be you should use bcmath php module instead for this operations.
http://php.net/manual/en/book.bc.php
Just take care that this module uses strings as inputs.
I'm pretty sure, you use a 32-bit version. Here the max value is 4.294.967.296.

php function to check if number is divisible by 0

I have checked a bunch of posts on stackoverflow and on articles on google but none of them were able to answer my question. Here is my code (i've simplified it instead of posting my code)
$first = 10;
$second = 0; //comes from db row count
$total = !is_int($first/$second) ? 0 : $first/$second;
problem is when i do this I keep getting the Division by zero error. I have a bunch and $second isnt always 0, it can be any number. But it does come out to 0 since the row counts for whatever query it comes out as 0. Is there a safe way of checking to see if $first can be divided by $second without giving an error? I have tried # before the !is_int and that just breaks all other statements.
Try this:
$total = ($second == 0) ? 0 : $first / $second;
You can't divide by 0 it is undefined. If you want to handle division by 0 just check if the divisor isn't equals to 0. Or a safer way, chack if it is a positive integer:
$first = 10;
$dbRowCount = dbFunction();
if ($dbRowCount > 0) {
$total = $first / $dbRowCount;
} else {
//Error handling
}
The ternary structure can accept more than one condition. and it will work just as any other if condition, and won't try the second condition if the first fails.
So, just add it
$total = ($first!==0 && $second!==0 && !is_int($first/$second)) ? 0 : $first/$second;
You might want to try checking if your $Second variable is 0.
Something like:
$First = 10;
$Second = $row['table_column'];
if ($Second == 0) {
echo "Oops this will be an error";
}
else
$First/$second = $me;

PHP infinity loop [While]

I am having some problems with PHP.
I used while to sum a number's digits always that it has more than two digits, some how, it gets into an infinity loop.
e.g: 56 = 5 + 6 = 11 = 1+1= 2.
Here is the code:
$somaP = 0;
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Can anyone help me? Guess it is a simple mistake, but I couldn't fix it.
You need to reset the value of $somaP in your while loop.
Currently it continues to increase its value every time through the loop.
Try this:
$numPer = (string)$numPer; //$numPer = number calculated previously
while (strlen($numPer) > 1){
$somaP = 0;
for ($j = 0; $j < strlen($numPer); $j++){
$somaP = $somaP + (int)($numPer[$j]);
}
$numPer = (string) $somaP;
}
Take a look at this line:
$numPer = (string) $somaP;
It seems that the length of $somaP is never lesser (or equal) than 1. So the length of $numPer is never lesser (or equal) than 1.
What are you trying to do?
It's unclear to me.
This for example would add every number in a string together?
E.g "1234" = 1+2+3+4 = 10
$total = 0;
for($i = 0; i < strlen($string); $i++){
$total += $string[$i];
}
echo $total;
This looks cleaner I would say:
$numPer = 56;
while ($numPer > 9){
$numPer = array_sum(str_split($numPer));
}
echo $numPer;
PHP handles all string <> number conversions for you, so no need to do (string) on a number unless really needed.

php - rand() between the same values

I havn't found a solution for this.
I have the numbers 3 and 5.
How do I randomly choose one of those two numbers.
In PHP.
rand() or mt_rand() jsut has min and max parameters.
thank you for your help!
if(mt_rand(0,1)) {
echo 3;
} else {
echo 5;
}
Or reverse it, your choice.
Get a random number with rand() and set $value to it's new value depending on the random number.
$value = rand(0,1) == 0 ? 3 : 5;
Just do a rand between only 2 values then use an if. So:
$randval = rand(0,1);
if($randval == 0)
$value = 3;
else
$value = 5;
You know that you have 2 values so you can do something like this:
$rand = rand(1,2);
if ($rand == 1)
$val = 3;
else
$val = 5;

Categories