0

Why doesn't this work:

echo (function() {
    if (strlen($_POST["a"]) > 150) {
        return "123";
    }

    return "abc";
})();

is there anyway to do this in PHP?

Like in JS you can do:

var x = (function() {return "asd";})();
Toumash
  • 1,077
  • 11
  • 27

1 Answers1

1

You can do this, just not with the elegant syntax of Javascript. You need call_user_func:

echo call_user_func(function() {
    if (strlen($_POST["a"]) > 150) {
        return "123";
    }

    return "abc";
});
lonesomeday
  • 233,373
  • 50
  • 316
  • 318