1

This code:

if(!empty(trim($_POST['post']))){ }

return this error:

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

How can I resolve and avoid to do 2 checks ( trim and then empty ) ?

I want to check if POST is not only a blank space.

xRobot
  • 25,579
  • 69
  • 184
  • 304
  • 2
    Did you actually read [the documentation page](http://php.net/manual/en/function.empty.php)? It clearly states that only variables work with `empty()` in PHP < 5.5 – nice ass May 02 '13 at 14:41
  • See this answer: http://stackoverflow.com/a/2173318/561731 – Naftali May 02 '13 at 14:42
  • I want to check if POST is not only a blank space. – xRobot May 02 '13 at 14:43
  • Why not just `trim($_POST['post']) !== ""`? `trim` `$_POST` values can only be strings, and `trim` will cast to string anyway. – cmbuckley May 02 '13 at 14:44
  • @OneTrickPony the funniest thing is what sample in warning use exact trim function to display problem:) – Narek May 02 '13 at 14:46

5 Answers5

3

you cant use functions inside isset , empty statements. just assign the result of trim to a variable.

$r = trim($_POST['blop']);

if(!empty($r))....

edit: Prior to PHP 5.5

mpm
  • 20,148
  • 7
  • 50
  • 55
2
if (trim($_POST['post'])) {

Is functionally equivalent to what you appear to be trying to do. There's no need to call !empty

AD7six
  • 63,116
  • 12
  • 91
  • 123
2
if (trim($_POST['post']) !== "") {
    // this is the same
}
cmbuckley
  • 40,217
  • 9
  • 77
  • 91
1

In the documentation it actually explains this problem specifically, then gives you an alternate solution. You can use

trim($name) == false.
christopher
  • 26,815
  • 5
  • 55
  • 89
0

In PHP, functions isset() and empty() are ment to test variables.

That means this

if(empty("someText")) { ... }

or this

if(isset(somefunction(args))) { ... }

doesn't make any sence, since result of a function is always defined, e.t.c.

These functions serve to tell wether a variable is defined or not, so argument must me a variable, or array with index, then it checks the index (works on objects too), like

if(!empty($_POST["mydata"])) {
    //do something
} else {
    echo "Wrong input";
}
kajacx
  • 12,361
  • 5
  • 43
  • 70