views:

76

answers:

2

I want to check in zend, whether a posted form value 'name' contains a value.For this i have used the following code

one method

if ($this->_getPatram('name') != null ) {
  echo 'field name contains value';
} else {
  echo 'field name contains  null value';
}

second method

if ($this->_hasParam('name')) {
 echo 'field name contains value';
} else {
  echo 'field name contains  null value';
}

output , when submitting the form with the 'name' field contains null value

in first method

field name contains null value (result is correct)

in second method

field name contains value (result is wrong)

So what is the difference between these two ? _hasParam and _getParam

+1  A: 

_hasParam() returns whether the param exists, and _getParam() returns the actual value. The difference lies in the fact that there are several values that are considered equal to null even if they do exist, such as 0 or '''. Use === or !== to compare instead.

Ignacio Vazquez-Abrams
ok, i got , what you said.But if it consider '' as false, the result of second method should come as 'field name contains null value', but the answer comes is 'field name contains value'Let me know please, why is happening so?
Linto davis
I misread the question slightly. Answer amended.
Ignacio Vazquez-Abrams
+1  A: 

$this->_hasParam('name') returns true because $this has a parameter by the name name.

field name contains value (result is wrong)

The result is correct. $this object in fact has a name field - The fact that its value happens to be null is not the concern of _hasParam function.

Use _hasParam to check if the object has a particular parameter or not and _getParam to get the value of that parameter.

For example, if you want to check if the submitted form has a property by the name foo, use _hasParam("foo"). To get the value of foo, use _getParam("foo")

Amarghosh
so if use the second method for checking any of the posted form values, it will return always true irresptive of whether it contains value or notlet me please know your comment
Linto davis
@Linto updated - hope it is clear.
Amarghosh
thank you very much, i understand every thing
Linto davis