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.
1 2 |
$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.
1 2 3 |
$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.
How to install LAMP on ubuntu.
I am using Mac so here is terminal command to unlink php 5.4 and install php 5.6 version.
1 2 3 4 5 6 7 |
/* Unlink php 5.4 */ $ brew unlink php54 /* Install php 5.6 */ $ brew install php56 |
To check current php version.
1 |
$ php -v |