-1

Having to backport a PHP 7 app to make it compatible with PHP 5.4 and I'm getting the following error :

<b>Parse error</b>:  syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in blah blah...

With this conditional statement in one of my functions :

if (!empty(MY_CONSTANT)) {
   // blah...
}

It's been a long time since I used PHP 5.4, why is this error being thrown?

spice
  • 1,442
  • 19
  • 35
  • Why the down vote without an explanation? You really think that helps? – spice Apr 26 '18 at 02:22
  • 1
    The downvote is probably because (a) you haven't shown us enough context to answer your question and / or (b) "unexpected ')'" is a basic syntax error that has been [thoroughly covered already](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them/18092308#18092308). – ChrisGPT was on strike Apr 26 '18 at 02:34
  • 1
    This is a legit question and shouldn't be downvoted. Your parenthesis pairing is fine. But running empty on a constant instead of a variable will give you that syntax error. – Schien Apr 26 '18 at 02:35
  • @Chris the down vote happened within 10 seconds of me posting the question, and as answered by Tien below, you can see that anybody who knew what they were talking about would have known that my question was completely valid and required no more code than was supplied. Some people are just very trigger happy on here sometimes. – spice Apr 26 '18 at 02:36
  • 1
    @Chris this is an interesting case because it is a parse error yet not a syntax error. it almost makes a good bar chat topic ;) – Schien Apr 26 '18 at 02:37
  • 1
    @spice I have upvoted to restore the balance – Schien Apr 26 '18 at 02:38
  • Haha and now I have 2 down votes.. Some people need to get out more... – spice Apr 26 '18 at 02:38
  • @Schien I appreciate that bro but it looks like the trigger happy crew are out in full effect tonight :D – spice Apr 26 '18 at 02:39
  • @Schien, indeed. This was a more interesting question than it first appeared. Though it's still a duplicate. (Before you jump on me for downvoting, I didn't.) – ChrisGPT was on strike Apr 26 '18 at 02:41
  • @Schien I tell you what, It would have taken me ages to figure this out for myself. It's been so long since I used 5.4. That would have kept me up for hours lol. – spice Apr 26 '18 at 02:42

2 Answers2

3

Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error.

If you'd like to check whether a given named constant exists you could use defined()

if ( defined('MY_CONSTANT') ) {
  // blah
}
Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • True but it doesn't help in my particular use case where I'm checking to see if the constant has been left blank or not. Consider the following : `const MY_CONSTANT = ""; if ( defined('MY_CONSTANT') ) { echo true; }` Will always return true even when `MY_CONSTANT` is empty. – spice Apr 26 '18 at 12:58
0

I try this & it's work on PHP 5.4.0

  define("MY_CONSTANT", 15);
  $testingVal = MY_CONSTANT; 
  if (!empty($testingVal)) {
      echo MY_CONSTANT;
  }