does this line of code work in php?

Discussion in 'PHP' started by PhilGlass, Feb 27, 2008.

  1. #1
    what i want to do is round up to the nearest even number
    example so if someone puts in 10.25 i want to to go up to 12

    
    if ( ($totalwidth % 2) != 0 ) {
    	$totalwidth = $totalwidth + 1;
    	}
    
    PHP:
    i was also given

    
    if (totalwidth % 2) {
            var totalwidth = ++totalwidth;
    }
    
    PHP:
    but that was for java and didn't work in php

    thank you for your time

    phil.
     
    PhilGlass, Feb 27, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    That won't work for 10.25. PHP returns 10.25 as being equal to 0, if you use 11.25, it will add 1 but will display 12.25. The code below works to make 10.25 return 12 :
    
    $totalwidth = 10.25;
    $totalwidth = ceil($totalwidth);
    echo "$totalwidth <br> ";
    if ( ($totalwidth % 2) != 0 ) {
        $totalwidth = $totalwidth + 1;
        }
    echo "$totalwidth <br> ";
    
    Code (markup):
     
    shallowink, Feb 27, 2008 IP
  3. PhilGlass

    PhilGlass Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wow! i didn't even know that ceil() existed! thanks again I appreciate the help.

    I have a quick question for you do you knokw of any good places to learn php? other then bothering people on the forums?

    thanks again shallowink!
     
    PhilGlass, Feb 28, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    The usual places, w3schools.com, tizag.com, php-mysql-tutorial.com and php.net for reference. Lots of stuff on all of those, the tizag.com is good but its kind of dated. But those are probably the best. Once you get a good idea of the terminology google searches turn up answers faster than anything else. And bugging people on the forums is fine, just be sure to show some effort/code and most times its fine.

    Good Luck.
     
    shallowink, Feb 28, 2008 IP
  5. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #5
    http://www.w3schools.com/php/php_ref_math.asp

    The main thing is knowing what to look for. In your case you were looking for math functions. So you pop "php math functions" into Google and you get that url as the first result. That page gives you every PHP math function and what it does.

    You want to be familiar with those in any language.
     
    KalvinB, Feb 28, 2008 IP