-2

I'm having some trouble with this function in PHP. I have a changing amount of posted items on this page, and I'm returning the variable in the array with this function:

function grade($a) {
    if ($a == 1) {
        return $_POST['1'];
    }
    if ($a == 2) {
        return $_POST['2'];
    }
    if ($a == 3) {
        return $_POST['3'];
    }
    if ($a == 4) {
        return $_POST['4'];
    }
    if ($a == 5) {
        return $_POST['5'];
    }
    if ($a == 6) {
        return $_POST['6'];
    }
    //... you get the idea
}

This is probably not the right way. How can I make this function shorter so that I don't have to make the if-statement for every possible array variable?

Thanks for you help!

Cubes
  • 3
  • 2
  • 3
    Generally you should not name fields as numbers. You should just call the field grade so that you can just get it out of the post `echo $_POST["grade"];`. – Logan Murphy May 08 '14 at 19:41
  • 5
    what is this? an attempt at setting the world record for the question with the most amount of answers that say exactly the same? – martskins May 08 '14 at 19:45
  • 2
    @lascort I think most of the people answering are trying to relive the experience of performing trivial and nonsensical tasks that only a classroom environment could dish out. – MonkeyZeus May 08 '14 at 19:50

7 Answers7

7

You can accomplish this very easily by using the value of the variable as the array key:

function grade($a) {
    if (!array_key_exists($a, $_POST)) {
        // Given grade key does not exist in the $_POST array
        return null;
    }
    return $_POST[$a];
}

You will note that you do not have to convert the integer to a string, as per the documentation:

Additionally the following key casts will occur:

  • Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8.
Community
  • 1
  • 1
3

Pretty simple, you just pass the variable on to the return:

function grade($a) {
    if( !isset( $_POST[$a] ) )
        return 0;
    return $_POST[$a];
}

This also checks if it doesn't exist, to return 0 as your grade.

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
2

You could use a switch operator : http://www.php.net/manual/fr/control-structures.switch.php

I'm not using php but this may work as well :

function grade($a) {
    return $_POST['' . $a];
}
Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
Alexandre FILLATRE
  • 1,305
  • 11
  • 20
  • This is the same as doing `return $_POST[$a]`. How does this answer the question? – Amal Murali May 08 '14 at 19:47
  • it does answer the question, it concatenates a useless empty string though, but it does answer the question (as all of the other answers in here already) – martskins May 08 '14 at 19:48
  • I didn't know that $a could be read as a String or a char without that concatenation. Now I do, and that strval($a) does it too. Thanks – Alexandre FILLATRE May 08 '14 at 19:52
2

make it simple:

function grade($a) {
  return isset($_POST[$a]) ? $_POST[$a] : '' ;
}
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
2

Ternary if would be the shortest.

function grade($a)
{
    return isset($_POST[$a])? $_POST[$a] : null;
}
0

Your can write like bellow

function grade($a) { 
    return $_POST[$a];
}
Logan Murphy
  • 6,120
  • 3
  • 24
  • 42
Anowar Hossain
  • 583
  • 4
  • 17
0
function grade($a) {
    if (isset($_POST[$a])) {
        return $_POST[$a];
    }
    return false;
}
shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50