SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to find a percentage

    Hi all,

    I have written the following code to work out a percentage:

    PHP Code:
    echo 'Percentage: '. ($numm/$number) * 100
    $numm = 3
    and
    $number = 4

    in this case.

    However this is just outputting "0" and nothing more. I've tried echoing the variables themselves to check they are set, and they are.

    Is there something I'm missing here?

    Thanks in advance

    -Sam
    Sam Hastings

  2. #2
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    United States
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I belive you are missing the ; at the end of your variable declarations. I tested and this worked fine.

    PHP Code:
    <?php
    $numm 
    3;
    $number 4;
    echo 
    'Percentage: ', ($numm/$number) * 100 '%';
    ?>
    Is there any other info you can provide?

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $numm = 3 part wasn't part of the script, it was just to let everyone else know what the values of each of the variables were (hence it not being in PHP tags).

    It still doesn't want to work

    -Sam
    Sam Hastings

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yah, it's weird, but try this:
    Code:
    echo 'Percentage: '. number_format($numm/$number*100,2).'%';
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    SitePoint Enthusiast CRYSTL's Avatar
    Join Date
    Sep 2003
    Location
    Kazakhstan
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe it's just because it cannot be used inline?

    Try this:
    PHP Code:
    $percentage =  $numm/$number*100;
    echo 
    "Percentage: $percentage %"


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
  •