3

I am developing a custom module in prestashop. In that I have taken value inside a function like this

$fname = !empty(Tools::getValue('fname')) ? Tools::getValue('fname') : '';

but its showing error like this

Fatal error: Can't use function return value in write context

So can someone tell me why the error is here? How to solve this issue?

NewUser
  • 12,713
  • 39
  • 142
  • 236
  • Possible duplicate of [Can't use method return value in write context](http://stackoverflow.com/questions/1075534/cant-use-method-return-value-in-write-context) – Aziz Saleh Feb 13 '16 at 03:34
  • NewUser please mark and up-vote the answer, if it solves your problem, thanks. it will help others – Alive to die - Anant Feb 13 '16 at 06:17
  • @A-2-A thanks for the answer but that did not worked. This worked for me `Tools::getValue('fname') ? Tools::getValue('fname') : ' ';` – NewUser Feb 13 '16 at 10:16

2 Answers2

0

I got the answer. It should be like this

Tools::getValue('fname') ? Tools::getValue('fname') : ' ';

NewUser
  • 12,713
  • 39
  • 142
  • 236
0

I just will explain it here for someone from google results.

  1. empty(someFunction()) creates error, because

Note: Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

http://php.net/manual/en/function.empty.php

  1. In Prestashop method Tools::getValue() have possibility to use default value,

    public static function getValue($key, $default_value = false) {

so you can use Tools::getValue('fname', ''); and returned value will contain or value of 'fname' or empty string, in this case.

Serge P
  • 1,863
  • 13
  • 14