SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: integers in php

  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question integers in php

    I'm trying to use integers in PHP but can't get anything to work. Am I using it correctly? Here's my php:

    PHP Code:
        if (($frac1 int($frac1))) {
        
    $decRes = ($_POST['frac1'] / $_POST['frac2']) + 1;
        }
        else {
        
    $message "<font color=\"red\">You did not fill in both fields!</font>";
        
    $decRes "";
        } 
    Many thanks for any help

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You want to make sure it's an int?

    In which case you need to use the comparing operator ==, not the assignment operator =.

    PHP Code:
    if (($frac1 == int($frac1))) { 
        
    $decRes = ($_POST['frac1'] / $_POST['frac2']) + 1
        } 
        else { 
        
    $message "<font color=\"red\">You did not fill in both fields!</font>"
        
    $decRes ""
        } 
    However the following would be better:
    PHP Code:
    if(is_numeric($frac1) && is_numeric($frac2)){ 
        
    $decRes = ($frac1 $frac2) + 1
    } else { 
        
    $message "<font color=\"red\">You did not fill in both fields!</font>"
        
    $decRes ""

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    You want to make sure it's an int?

    In which case you need to use the comparing operator ==, not the assignment operator =.

    PHP Code:
    if (($frac1 == int($frac1))) { 
        
    $decRes = ($_POST['frac1'] / $_POST['frac2']) + 1
        } 
        else { 
        
    $message "<font color=\"red\">You did not fill in both fields!</font>"
        
    $decRes ""
        } 
    However the following would be better:
    PHP Code:
    if(is_numeric($frac1) && is_numeric($frac2)){ 
        
    $decRes = ($frac1 $frac2) + 1
    } else { 
        
    $message "<font color=\"red\">You did not fill in both fields!</font>"
        
    $decRes ""

    Id suggest using ctype_digit() instead of is_numeric() as is_numeric() will return true on decimal and numeric strings (+0123.45e6), also returns true on hex (0xFF)

    http://au.php.net/is_numeric
    chrome is a wrapper that combines a browser with spyware

  4. #4
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd go for
    $frac1=(int)$frac1;
    $f1=(int)$_POST['frac1'] ;
    $f2=(int)$_POST['frac2'];
    if ($frac1<1)
    {
    $decRes = ($f1 / $f2) + 1;
    }
    else
    {
    $message = "<span style='color:red'>You did not fill in both fields!</span>";
    $decRes = '';
    }

  5. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, thanks for all of these suggestions however none seem to work. Check this code I have substituted the (int) here for each of the recommendations and $fracRes1 and FracRes2 are always returning as 5 and not altering according to whether $dec is an integer (whole number) or decimal. Can someone help me with this?

    PHP Code:
        if (($_POST['frac1'] != "") && ($_POST['frac2'] != "")) {
        if (
    $dec == (int)$dec) {
        
    $fracRes1 5;
        
    $fracRes2 5;
        }
        else {
        
    $fracRes1 4;    
        
    $fracRes2 4;
        }
        }
        else {
        
    $message "<font color=\"red\">You did not fill in both fields!</font>";
        
    $result "N/A";
        } 

  6. #6
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, without the rst of the code I'm not really sure what you're trying to do here.

    If you want another way of testing if a number is a whole number or has a decimal part, you might try the is_int function. However - I think this will return false if you are testing a variable from a form input, which is a string and thus not a float or int, whatever it's value.

    One way to test if an number is an integer is to take advantage of type coercion in arithmetic functions. If you divide a number, it will return an int if whole, or a float if not. PHP will convert a string into a number to do so.

    So, try dividing the number you want to test by 1, and then checking if the result is a float or an int.

    $p='23.23';
    $i=$p/1;
    var_dump($i);
    float(23.23)

    $p=24;
    $i=$p/1;
    var_dump($i);
    int(24)

  7. #7
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    OK after much deliberation and testing I found that I should have of course been using elseif and that was the reason that none were working. Subsequently I used the (int) technique which was successful. So thanks to all..

    A rather complex calculation has since caused me to use 10 elseifs' in the same if statement, which I don't think is possible. Can anyone tell me how I get PHP to allow me to include these in the calculation?

    Regards and thanks to one and all..

  8. #8
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    You can use as many elseifs as you'd like, that's no problem. Some people find using a 'switch' is more clear, but it's up to you. A switch goes like

    Code:
    switch($variable_to_switch_on)
       {
       case(1):{do_this();break;}
       case(2):{do_that();break;}
       default:{glark();break;}
       }
    where it's checking if the var equals the number in parentheses for cases. It's possible to have more complex checking, as well. If you don't include the break, the interpreter will keep checking the following conditions, and execute more than one if applicable. You can see full documentation on this at the php site, of course.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    g_force, what exactly are you trying to do? You need to make sure people understand the realm of your problem or task if you want to get a good answer.

  10. #10
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I'm just trying to use a number of conditions within the 'if' statement.


    PHP Code:
    if (($_POST['frac1'] == "") && ($_POST['frac2'] == "")) {
    $message "<font color=\"red\">Please fill in both fraction fields!</font>";
    }
        elseif (
    $_POST['dec'] == "") {
        
    $fracRes1 $_POST['frac1'];
        
    $fracRes2 $_POST['frac2'];    
        }
        elseif ((
    $_POST['dec'] <= 1) && ($_POST['dec'] > 0)) {
        
    $fracRes1 "";
        
    $message "<font color=\"red\">Please enter a decimal bigger than 1!</font>";
        }
        elseif ((
    $_POST['dec']-1) == (int)($_POST['dec']-1)) {
        
    $decRes $_POST['dec'];
        
    $fracRes1 $decRes-1;
        
    $fracRes2 = ($decRes-1)/($decRes-1);
        }
        elseif ((((
    $_POST['dec']-1)*100)/50) == (int)((($_POST['dec']-1)*100)/50)) {
        
    $decRes $_POST['dec'];
        
    $fracRes1 = (($decRes-1)*100)/50;
        
    $fracRes2 = ((($decRes-1)*100)/50)/($decRes-1);
        }
        elseif ((((
    $_POST['dec']-1)*100)/25) == (int)((($_POST['dec']-1)*100)/25)) {
        
    $decRes $_POST['dec'];
        
    $fracRes1 = (($decRes-1)*100)/25;
        
    $fracRes2 = ((($decRes-1)*100)/25)/($decRes-1);
        }
        elseif ((((
    $_POST['dec']-1)*100)/20) == (int)((($_POST['dec']-1)*100)/20)) {
        
    $decRes $_POST['dec'];
        
    $fracRes1 = (($decRes-1)*100)/20;
        
    $fracRes2 = ((($decRes-1)*100)/20)/($decRes-1);
        }
        elseif ((((
    $_POST['dec']-1)*100)/10) == (int)((($_POST['dec']-1)*100)/10)) {
        
    $decRes $_POST['dec'];
        
    $fracRes1 = (($decRes-1)*100)/10;
        
    $fracRes2 = ((($decRes-1)*100)/10)/($decRes-1);
        }
        elseif ((((
    $_POST['dec']-1)*100)/5) == (int)((($_POST['dec']-1)*100)/5)) {
        
    $decRes $_POST['dec'];
        
    $fracRes1 = (($decRes-1)*100)/5;
        
    $fracRes2 = ((($decRes-1)*100)/5)/($decRes-1);
        }
        elseif ((((
    $_POST['dec']-1)*100)/4) == (int)((($_POST['dec']-1)*100)/4)) {
        
    $decRes $_POST['dec'];
        
    $fracRes1 = (($decRes-1)*100)/4;
        
    $fracRes2 = ((($decRes-1)*100)/4)/($decRes-1);
        }
        elseif ((((
    $_POST['dec']-1)*100)/2) == (int)((($_POST['dec']-1)*100)/2)) {
        
    $decRes $_POST['dec'];
        
    $fracRes1 = (($decRes-1)*100)/2;
        
    $fracRes2 = ((($decRes-1)*100)/2)/($decRes-1);
        }
        else {
        
    $decRes $_POST['dec'];
        
    $fracRes1 = (($decRes-1)*100)/1;
        
    $fracRes2 = ((($decRes-1)*100)/1)/($decRes-1);
        } 
    This only works up to the 5th 'elseif' statement and after that returns the 'else' result. I hope this is a little clearer. If not please say.

    _Thanks and regards all


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
  •