-1

I'm trying to pass user input from HTML form to following function in PHP

but it gives following error right at first line as soon as i put brackets [] at first line after $_POST.

Parse error: syntax error, unexpected '[', expecting ')' in C:\xampp\htdocs\project1\SITE3.php on line 175

My code is following:

function setrating($_POST["rate"]){
        if($_POST["rate"]=="PG"){

            echo"hi";
        }

        else{
            echo"A";
        }
    }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • You don't need it like that. `$_POST` is superglobal so it is available in all scopes. just do `function setrating(){...` – user3783243 Jul 19 '18 at 02:34

1 Answers1

1

You can't define a function with the variable $_POST['rate']. It needs have a variable in the definition. Like so:

    function setrating($rate) {
       if($rate == "PG" || $rate == "G" || $rate == "PG-13" || $rate == "R" || $rate == "NR") {
           $this->rate = $rate;
       } else {
           $this->rate = "NR";
       }
    }    

Then you can call it with:

    $classVar = new YourClass();
    $classVar->setrating($_POST['rate']);
MC1010
  • 106
  • 5