tags:

views:

88

answers:

3

What will the code below be if value is 2 and counter is 11? What would $rating be?

$rating = (@round($rs[value] / $rs[counter],1)) * 10; 

The complete code is below.

function getRating(){
    $sql= "select * from vote";
    $result=@mysql_query($sql);
    $rs=@mysql_fetch_array($result);
    $rating = (@round($rs[value] / $rs[counter],1)) * 10; 
    echo $rating;
}
+3  A: 

If $rs['value']=2 and $rs['counter']=11 then

2/11 = .18181818181818
round(.181818181, 1) = .2 // This uses the ever popular rule of thumb. 
.2 * 10 = 2

I believe. If not, I'm really screwed in AP Statistics...

BTW RTFM. :P

Chacha102
+3  A: 

Well, that actually depends on what $rs[value] and $rs[counter] contain rather than value and counter themselves. But my question to you is:

What did it return when you tried it?

That'll probably give you the right answer, surprisingly enough, rather than having one of us do it for you.


Responding to your comment:

I was just checking if I did right. I couldn't figure out why I added the ",1" to my code. I completely forgot so I wanted to check, maybe that should of been my question?

Maybe, but there's ample precedent on StackOverflow where the question that was asked is frequently replaced, over time, by the question that was meant. That's not a problem and the best help-desk-type people (including those, like myself, who look after countless family computers) are those who can tease out the real question without insulting the intelligence of the asker :-)

In any case, the 1 argument to the round function specifies how many fractional places are used to round the number, so:

round (1.2341234,0) = 1
round (1.2341234,1) = 1.2
round (1.2341234,2) = 1.23
round (1.2341234,3) = 1.234
round (1.2341234,4) = 1.2341

and so on.

paxdiablo
+1 "What did it return when you tried it?"
Shadi Almosri
I was just checking if I did right I couldn't figure out why I added the `,1` to my code completely forgot so I wanted to check, maybe that should of been my question?
Jawa
@Jawa, the ",1" rounds the number to that many fractional places.
paxdiablo
A: 

Have you tried placing the literal values in it?

$rating = round(2 / 11,1) * 10; 
var_dump($rating);

0.18181 * 10 == 1.8181, which is being rounded up to 2

Edit:

Also, error suppression on round()? Really? $rs[value] should be $rs['value']

pygorex1