views:

164

answers:

2

Inside the begining of a function I have this:

if(false);
{
    return 'TRUE';
}

it is returning "TRUE"! Obviously my real use was checking a more useful expression and returning something else. I just changed it to this to elaborate my point.

Why is this happening? Can you not put a return inside an if statement? I do this all the time in other languages.

For example

instead of this:

function () {
if(something)
{
//process stuff
}
}

which requires wraping everthing inside the function inside an if.

I prefer to do this:

function() {
if(!something)
return;
//process stuff
}

Is this not OK in PHP... is there a work around? Or am I just crazy?

+17  A: 

You're just crazy. :)

if(false); //   <----- remove semi colon
{
    return 'TRUE';
}

should have one less semi-colon.

if(false)
{
    return 'TRUE';
}
Dutchie432
Wow do I feel like a jackass now. Its been a really long day.. but I should have seen that before posting.
John Isaacks
+1 for spotting a mistake which can easily eat up hours near-instantly. Well, maybe it becomes a habit to look for semicolons when facing this kind of bug...
delnan
if this is supposed to be a [boolean](http://de2.php.net/manual/en/language.types.boolean.php), it should read `TRUE`, without the `'`
Gordon
@Gordon, its not supposed to be a boolean, I am echoing out what the function returns and I just quickly changed it to "true" so I could visually see if the `if` was returning true.
John Isaacks
On a side note, I always put the opening "{" bracket on the same line as my IF statement. I find it easier to distinguish that it's an if line just by skimming the right-most characters.
Dutchie432
+7  A: 

You have an extra semicolon after the if condition.

James Roth