2

I seems that there is a limitation to using an inline if statement in PHP when you want to return.

function example($Variable)
{
    ( (int) (++$Variable) == 1 ) ? return true : return false;
}

$test = example(1);

Expected: false

This gives an error which is strange because I have never seen this before.

Error type : 4

Message : syntax error, unexpected 'return' (T_RETURN)

Can anyone explain why you cannot use the return inside an if statement like this?

Community
  • 1
  • 1
Jaquarh
  • 6,493
  • 7
  • 34
  • 86

1 Answers1

8

The ternary already returns the results. You can't return a return function.

You can make return before variable result like this:

function example($Variable)
{
    return ( (int) (++$Variable) == 1 ) ? true : false;
}
$test = example(1);
Phiter
  • 14,570
  • 14
  • 50
  • 84
DevLoots
  • 747
  • 1
  • 6
  • 21