[Solved-4 Solutions] Weird PHP error: 'Can't use function return value in write context'
Error Description:
Weird PHP error: 'Can't use function return value in write context'
Solution 1:
- This also happens when using empty on a function return:
!empty(trim($someText)) and doSomething()
click below button to copy the code. By - php tutorial - team
- because empty is not a function but a language construct (not sure), and it only takes variables:
empty($someVar)
click below button to copy the code. By - php tutorial - team
- Since PHP 5.5, it supports more than variables. But if we need it before 5.5, use trim($name) == false.
Solution 2:
if (isset($_POST('sms_code') == TRUE ) {
click below button to copy the code. By - php tutorial - team
- change this line to
if (isset($_POST['sms_code']) == TRUE ) {
click below button to copy the code. By - php tutorial - team
- we are using parentheseis () for $_POST but we wanted square brackets []
- OR
if (isset($_POST['sms_code']) && $_POST['sms_code']) {
//this lets in this block only if $_POST['sms_code'] has some value
click below button to copy the code. By - php tutorial - team
Solution 3:
- This can happen in more than one scenario, below is a list of well known scenarios :
// calling empty on a function
empty(myFunction($myVariable)); // the return value of myFunction should be saved into a variable
// then you can use empty on your variable
// using parenthesis to access an element of an array, parenthesis are used to call a function
if (isset($_POST('sms_code') == TRUE ) { ...
// that should be if(isset($_POST['sms_code']) == TRUE)
click below button to copy the code. By - php tutorial - team
- This also could be triggered when we try to increment the result of a function like below:
$myCounter = '356';
$myCounter = intVal($myCounter)++; // we try to increment the result of the intVal...
// like the first case, the ++ needs to be called on a variable.
click below button to copy the code. By - php tutorial - team
Solution 4:
- PHP Fatal error: Can’t use function return value in write context in …/wp-content/themes/customizr/inc/czr-init.php on line 441
if ( ! function_exists( ‘czr_fn_has_social_links’ ) ) {
function czr_fn_has_social_links() {
return !empty( czr_fn_get_opt(‘tc_social_links’) );
}