Home >Backend Development >PHP Tutorial >Why Does PHP Throw a \'Can\'t Use Function Return Value in Write Context\' Error?
The error message "Can't use function return value in write context" indicates that PHP is unable to assign a function's return value to a variable or write it to a file. This error commonly occurs when trying to evaluate a function's return directly in a conditional statement.
In the provided example:
The error appears on line 48, which reads:
if (isset($_POST('sms_code') == TRUE ) {...}
Here, PHP is attempting to evaluate the truthiness of isset($_POST('sms_code')) and assign the result to TRUE. However, isset() is a language construct, not a function, and its return value cannot be used in this context.
Solution:
To resolve this error, directly assign the return value of isset() to a boolean variable:
$sms_code_exists = isset($_POST('sms_code')); if ($sms_code_exists) {...}
Additional Note:
This error can also occur when using the empty language construct on a function return value. For example:
!empty(trim($someText)) and doSomething()
In such cases, it is correct to evaluate the return value of trim() using the comparison operator:
trim($someText) !== '' and doSomething()
The above is the detailed content of Why Does PHP Throw a \'Can\'t Use Function Return Value in Write Context\' Error?. For more information, please follow other related articles on the PHP Chinese website!