Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By sunfighter
  • 1 Post By gvre

Thread: CONVERTING IF and ELSEIF statement into Function Statement

  1. #1
    New Coder
    Join Date
    Apr 2012
    Location
    California
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts

    CONVERTING IF and ELSEIF statement into Function Statement

    Friends,
    I am asking your help how can i convert the statement below as a function statement. Let say the function name is "whats_your_grade" and the example grade is "C".

    Please advise also how to call the function and show in a web page.


    $letter_grade = C ;

    if ($letter_grade == 'A') {
    echo 'well above average';

    } elseif ($letter_grade == 'B') {
    echo 'above average';

    } elseif ($letter_grade == 'C') {
    echo 'average';

    } elseif ($letter_grade == 'D') {
    echo 'below average';

    } elseif ($letter_grade == 'F') {
    echo 'failed';

    } else {
    echo 'invalid grade';
    }

    I am very new in PHP and self learning it. I am trying to compare different statements with the same data so I can understand thoroughly php scripting. THANK YOU IN ADVANCE.

    Jose E.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    i would use the SWITCH function:
    if this code were in a file called test.php put this at the end:

    test.php?grade=A

    PHP Code:
    <?PHP
    switch ($_GET['grade']){
        case 
    'A':{echo 'well above average';}
        case 
    'B':{echo 'above average';}
        case 
    'C':{echo 'average';}
        case 
    'D':{echo 'below average';}
        case 
    'F':{echo 'failed';}
        default:{echo 
    'invalid grade'; }
    }
    ?>
    Last edited by Arcticwarrio; 03-07-2014 at 02:44 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    espi62 (03-14-2014)

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,445
    Thanks
    23
    Thanked 630 Times in 629 Posts
    Not sure what you mean by
    and show in a web page
    But this is what I would do:
    PHP Code:
    <?php
    $response 
    "";
    $letter_grade "";

    if(isset(
    $_POST["letter"])){
        
    $letter_grade strtoupper($_POST["letter"]);
        
    $response grade_def($letter_grade);
    }

    function 
    grade_def($n){
        switch (
    $n){
        case 
    "A":
          return 
    'well above average';
          break;
        case 
    "B":
          return 
    'above average';
          break;
        case 
    "C":
          return 
    'average';
          break;
        case 
    "D":
          return 
    'below average';
          break;
        case 
    "F":
          return 
    'invalid grade';
          break;
        default:
          return 
    "Something has gone wrong";
        }
    }
    ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
    span{
        color: red;
    }
    </style>
    </head>

    <body>
    <form method="POST" action="#" id="my_form">
        Enter the letter grade:
        <input type="text" name="letter" maxlength="4" value="<?php echo $letter_grade?>" >
        <span id="graded"><?php echo $response?></span>
    </form>
    </body>
    </html>
    espi62 likes this.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    espi62 (03-14-2014)

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    PHP Code:
    function getGradeDescription($grade)
    {
        
    $grade strtoupper($grade);
        
    $grades = array(
            
    'A' => 'well above average',
            
    'B' => 'above average',
            
    'C' => 'average',
            
    'D' => 'below average',
            
    'F' => 'failed',
        );

        return isset(
    $grades[$grade]) ? $grades[$grade] : 'invalid grade';
    }

    $letter_grade 'C';
    echo 
    getGradeDescription($letter_grade); 
    OR
    PHP Code:
    function getGradeDescription($grade)
    {
        switch(
    strtoupper($grade))
        {
            case 
    'A': return 'well above average';
            case 
    'B': return 'above average';
            case 
    'C': return 'average';
            case 
    'D': return 'below average';
            case 
    'F': return 'failed';
            default: return 
    'invalid grade';
        }
    }

    $letter_grade 'C';
    echo 
    getGradeDescription($letter_grade); 

  • Users who have thanked gvre for this post:

    espi62 (03-14-2014)

  • #5
    New Coder
    Join Date
    Apr 2012
    Location
    California
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you very much for your help

    espi62

  • #6
    New Coder
    Join Date
    Apr 2012
    Location
    California
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you for your help

    espi62


  •  

    Tags for this Thread

    Posting Permissions

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