PHP函数表单不起作用

Why is this php function not working? It does work when not using $element. With $element it always returns test.

function setVar($element)
 {
    if(isset($_POST['$element'])){
    $varname = $_POST['$element']; 
    } 
    else {
    $varname = 'test';
    }
    return $varname;
 }

 $var = setVar('element_6');

You probably mean:

... $_POST[$element] ...

without the quotes? Single-quoted content never gets replaced.

Change $_POST['$element'] in your code to $_POST[$element] and it should work fine.. $_POST['$element']) refers to nothing right now.

You'll need to change $_POST['$element'] to $_POST[$element]. Anything between single quotes is treated literally.

See: http://php.net/manual/en/language.types.string.php

You're referencing $_POST['$element']. Note that the single quotes around $element here turn it into a static string.

Solution: Remove the quote marks, and it will parse the $element properly.