Question

I'm writing a form-helper class in php, that should check if a value is submitted via the method, the form defined.

public function getValue($name) {
    $getFrom  = ($this->method == 'post') ? '_POST' : '_GET';
    return isset($$getFrom[$name]) ? $$getFrom[$name] : null;
}

$this->method is post or get, it's form-dependent. This method looks fine for me, but php throws a "Illegal string offset" warning. What can I do without using an if-block.

Thank you!

Was it helpful?

Solution

I know that the replies have been an attempt to help the user with a better approach, but they do not answer the question. Why is it throwing the error that he sees? I just tested this to ensure I was correct in my assumption.

When he uses $$getFrom[$name], PHP first parses "$getFrom[$name]". That is what is throwing the error. He obviously wants $getFrom to be parsed without including [$name]. So, he needs to use ${$getFrom}[$name].

OTHER TIPS

Try this,

public function getValue($name) {       
   return isset($_REQUEST[$name]) ? $_REQUEST[$name] : null;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top