Function to calculate percentage

Discussion in 'PHP' started by pradeep, Feb 21, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    I had to write the code to get percentage every time I needed it, so I thought it'd be much better to have a function for the purpose, here's the function I wrote.

    PHP:
    function percentage($int1$int2
     {
         
    $per $int1 $int2;
         
    $res $per 100;
         return 
    $res;
     }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    This is not a good way of doing it as you may not get correct results. I am not sure about it in PHP but let me know what is the output for the forllowing.
    $int1 = 48
    $int2 = 100
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Will return 48!
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    But it should not as 48/100 in integer should be 0
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    the arguments passed are integers, but $res is casted to int.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What is the type for $per
     
  7. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    By default there is not data type specified to a variable, it takes the data type of the value assigned. e.g. $a = 10/6; here $a becomes float, and $a = 10/2 here $a becomes int and $a = 'Shabbir' here $a becomes string.
    So in this case $per = $int1 / $int2; $per becomes float.
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I would suggest that the parameters shouldn't be ints, anyway. Someone might want to know the percentage of a fractional number. Someone also might think that the return type has been coerced to match an the input arguments (integers), rather than having been set by PHPs "autotyping".

    I would also suggest that the relationship, a/b*100, is far too simple to waste the overhead of a function call. On the other hand, I come from the days when resources weren't so abundant that one could be profligate with them.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice