0

So I'm submiting a form and I'm trying to check if a field is empty or not and I do this:

if(empty($this->_getParam('my_field')))

And It doesn't work, But this works if I just use $_POST['my_field]

<b>Fatal error</b>: Can't use method return value in write context in....

How should I do this with $this->_getParam in order to work? since I guess is not such a good option to use $_POST on a framework.

j0k
  • 22,600
  • 28
  • 79
  • 90
Uffo
  • 9,628
  • 24
  • 90
  • 154

4 Answers4

4

This is your problem: Can't use method return value in write context

Here's a good explanation why have to do this: Can't use method return value in write context

To use empty() you would have to first get the return value

$field = $this->_getParam('my_field');
if(empty($field)){
  //is empty
}

(Update: This is not an issue for PHP versions >= 5.5) empty($this->_getParam('my_field')) would work just fine.

aknosis
  • 3,602
  • 20
  • 33
1

empty - like isset - is not a real function, instead it's a language construct. So you can only use variables as its only argument (no expressions, no function results).

But, you can use a temporary variable:

$my_field = $this->_getParam('my_field');
if(empty($my_field))
....
pozs
  • 34,608
  • 5
  • 57
  • 63
1

The other answers are correct. An alernative would be to provide a default to your call to _getParam(), such as null. Then you could call

is_null($this->_getParam('myfield',null))

This method might be preferable, because Zend already determines that parameter's emptiness.

Seth Battin
  • 2,811
  • 22
  • 36
0

You can use temporary variable

$field = $this->_getParam('my_field');

if(empty($field)){
  //you code here
}
admoghal
  • 1,498
  • 1
  • 10
  • 5