SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If/Else basic help

    Code PHP:
    <?php
     $weight = "48";
     if ($weight < 45)
     {
      print ("Reject");
     }
     elseif ($weight >= 45 && < 60)
     {
      print ("small");
     }
    ?>

    Just started a PHP course, only in the 3rd week and have only just started coding. I know this is basic programming but having trouble with IF/ELSE statements. Only have two so far but will be adding another 4 or so. Only the first IF works tried a few things and research but without any help.

    Thanks
    Last edited by pritnep; Feb 26, 2009 at 22:40.

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $weight 
    48// no need to enclose integers in quotes, quotes are for enclosing strings

    if ($weight 45) {
        print (
    "Reject");
    } elseif (
    $weight >= 45 && $weight 60) {
    //                         ^^^^^^^
        
    print ("small");
    }
    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh thank you for the quick reply, been a long day and knew it would be something simple - that and I'm just starting out with PHP.

  4. #4
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add another else if statement, - meaning, split the second part into two else if statements.

  5. #5
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by janniesekind View Post
    Add another else if statement, - meaning, split the second part into two else if statements.
    That solution is a useless thing to do in this case, because he is checking if something is between 2 numbers, so the best way to do this is using the 2 numbers in a single elseif.

  6. #6
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you to use switch-case if you have multiple if-else statements. That will make your work easy and organized better.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I would of but I think we had to use if/else statements as the question after it required switch statement, so learn't a bit about how to use that instead.

  8. #8
    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)
    Ok, as you're new I'll just advise you to use echo '' rather than print ("").

    Also, the elseif will only be executed if the variable is above 45 anyway, so the first part of that if isn't required.

    So how I would write it:
    PHP Code:
    if ($weight 45){
        echo 
    'Reject';
    }else if(
    $weight 60){
        echo 
    'small';
    }else if(
    $weight 75){
        echo 
    'medium';
    }else if(
    $weight 90){
        echo 
    'large';

    An alternative to this would be using an array-based loop, which would be handy if you have many different values. For example:
    PHP Code:
    $WeightArray = array(45 => 'Reject'60 => 'Small'75 => 'Medium'90 =>'Large', -=> 'Too Heavy');
    foreach(
    $WeightArray as $WeightItem => $Output){ 
        if(
    $Weight $WeightItem || $WeightItem == -1){ 
            return 
    $Output
            break; 
        }

    This would only work if you're doing a similar operation no matter what the output is though.

    Being a beginner I wouldn't expect you to grasp the above right away, it's just an example of what you could do if you want to minimalise code or make it more expandable.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •