PHP Fatal Error Fix: Can’t use method return value in write context

Jan 09, 2011 Author: Dr_ViRuS

PHP Fatal Error Fix: Can’t use method return value in write context.

Very nice error in very bad moment but very very logical error. When i got this error in my php code i was confused

My php code was something like this

   
  if (!empty(some_method()))
  {......
   }

At first glance it looks like correct php code and i was very angry on my computer. But is not!
Why?

PHP compiler generate fatal error if you use function return value in read/write context. Although this is not applicable for all PHP supported function but PHP function like empty does not support use of the function in this way. In other words, php empty function cannot check the return value of a function or method. It can only check variables so use only variable inside empty function. Any other function or expression inside empty function will lead to generate fatal error.

Example of problem

    //Wrong
    if(empty(trim($testimony))) echo 'Empty'; else echo    'Not Empty';

    //Correct
    $testimony = trim($testimony);
    if(empty($testimony)) echo “Empty”; else echo 'Not Empty';

    Wrong
    if(empty($obj->get_results('post')) { // Processing Code }

    //Correct
    $tmp = $obj->get_results('post');
    if(empty($tmp)) { // Processing Code }


views 6728
  1. Add New Comment