SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,934
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    [PHP] Percentage Function

    I just wrote this modest function to calculate a percentage, and am curious what you guys think of my code...

    PHP Code:
        function getAgreePct($reviewCount$agreeCount){
            
    /**
             * Returns percentage of Reviews where members "agree".
             *
             * Written on: 2014-08-10
             * Updated on: 
             *
             * @param        Integer        $reviewCount
             * @param        Integer        $agreeCount
             *
             * @return    Double        $agreePct
             */
            
            // Cast arguments to Integer type.
            
    $reviewCount = (int)$reviewCount;
            
    $agreeCount = (int)$agreeCount;

            if ((empty(
    $reviewCount)) || (empty($agreeCount))){
                
    $agreePct 0;
            
            }else{
                
    $agreePct = ($agreeCount/$reviewCount) * 100;
            }
            
            return 
    $agreePct;

        }
    //End of getAgreePct 

    Both parameters could be NULL, 0, >0, (or Text if hacked).

    I probably don't need the Casting, but in case Text was submitted, it would catch things. (Can't help but be too paranoid!)

    Sincerely,


    Debbie
    Last edited by Pullo; Aug 11, 2014 at 10:15. Reason: Added [PHP] to title


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •