Solved

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

Posted on 2014-09-26
6
901 Views
Last Modified: 2014-09-26
Hi Experts,

Why do I keep getting:
Fatal error: Can't use function return value in write context

...when I:
   if(isset($_POST["email"]) || !empty(trim($_POST["email"])))
   {
       $strEmail = $_POST["email"];
   }
   else 
   {
       $err = 1;
   }

Open in new window


Thank you.
0
Comment
Question by:APD_Toronto
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40346657
if(isset($_POST["email"]) && !empty(trim($_POST["email"])))
0
 

Author Comment

by:APD_Toronto
ID: 40346664
makes logical sense - thanks, but same error
0
 
LVL 58

Expert Comment

by:Gary
ID: 40346668
What is the whole function
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:APD_Toronto
ID: 40346673
nothing too complex... The error is at Li.40

<?php

    $submit = '';
    $err = 0;
   
    $strName = '';
    $strEmail = '';
    $strSelect = '';
    $strMessage = '';
   
    
    if(isset($_GET["submit"]))
    {
        $submit = $_GET["submit"];
    }
 
    if($submit == '')
    {
        displayForm();
    }
    else 
    {
        validate();
    }

function validate()
{    
   global $err;
   
   global $strName;
   global $strEmail;
   global $strSelect;
   global $strMessage;
   
   if(isset($_POST["name"]))
   {
       $strName = $_POST["name"];
   }
   
   if(isset($_POST["email"]) && !empty(trim($_POST["email"])))
   {
       $strEmail = $_POST["email"];
   }
   else 
   {
       $err = 1;
   }
   
   if(isset($_POST["selection"]))
   {
       $strSelect = $_POST["selection"];
   }
   else
   { 
       $err = 1;
   }
   
   if(isset($_POST["message"]) && !empty(trim($_POST["message"])))
   {
       $strMessage = $_POST["message"];
   }
   else 
   {
       $err = 1;
   }
   
   if($err = 1)
   {
       displayForm();       
   }
   else
   {
       sendEmail();
   }    
}

Open in new window

0
 
LVL 8

Accepted Solution

by:
Ahmed Merghani earned 500 total points
ID: 40346674
Your code should be changed to something like this:
if(isset($_POST["email"]) && (trim($_POST["email"]) != false))
   {
       $strEmail = $_POST["email"];
   }
   else 
   {
       $err = 1;
   }

Open in new window

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.
See this link:
http://us3.php.net/manual/en/function.empty.php
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40346678
As a general rule, compound statements with multiple function calls in the same line of code are a path to confusion.  I believe this statement is causing the problem:

empty(trim($_POST["email"]))

Try something more like this:

$email = trim($_POST['email']);
if (empty($email)) /* DO SOMETHING */

Long ago, PHP allowed this sort of thing, but it's not allowed any more.  That's one of the risks of getting old bits of code from the internet - you don't really know if it works.  Maybe it used to work and doesn't any more.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question