I got following fatal error in php can’t use method return value in write context. When this fatal error comes i didn’t understand what’s wrong with my code, as everything looks fine in my code editor. So i started debugging what’s the root cause for this error.
PHP solution for Can’t use method return value in write context
I was using Yii framework. In my Yii application i have written following statement.
$userId = !empty( Yii::app()->request->getParam('user_id') ) ? ( Yii::app()->request->getParam('user_id') )
: 0 ;
To solve current fatal error i break single statement into two. Now the issue is resolved.
$userId = Yii::app()->request->getParam('user_id');
$userId = !empty ($userId) ? $userId : 0;
PHP manual clearly mentioned following things.
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.
To prevent this error other option is to upgrade your php version. My php version was PHP 5.4 . Now i upgraded to PHP version 5.6. After upgrading my php version this issue is automatically resolved.
I am using Mac so here is terminal command to unlink php 5.4 and install php 5.6 version.
/* Unlink php 5.4 */
$ brew unlink php54
/* Install php 5.6 */
$ brew install php56
To check current php version.
$ php -v