Welcome to WebmasterWorld Guest from 54.196.2.131

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Syntax error, hoping for a second set of eyes

     
6:20 am on Feb 25, 2014 (gmt 0)

Junior Member

joined:Jan 9, 2014
posts: 150
votes: 0


This has to be a simple error, but I can't find it. I'm hoping a second set of eyes can help.

Here's the script:

// defined earlier in a separately included script
$default = "charlie";
$lastmodified = "20140224111423";

// the script in question
$approved = false;
$sttMod = strtotime($lastmodified);

if ($default == "alpha") {
if ($sttMod >= strtotime("-30 days")) $approved = true;
}

else if ($default == "beta" || $default == "charlie") {
if ($sttMod >= strtotime("-90 days")) $approved = true;
}

else $approved = true;

echo $approved;

// returns true, which it should


But, if I remove the "unnecessary" { }, like so:

if ($default == "alpha")
if ($sttMod >= strtotime("-30 days")) $approved = true;

else if ($default == "beta" || $default == "charlie")
if ($sttMod >= strtotime("-90 days")) $approved = true;

else $approved = true;

// returns false

I'm getting "false" (or, at least, it's printing ""), which shouldn't be correct. This should still match the else if statement, and strtotime() is >= 90 days, so it should return true.

Where am I messing up?
6:37 am on Feb 25, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 15, 2004
posts:1867
votes: 0


But, if I remove the "unnecessary" { }, like so:


Why do you think the braces are unnecessary? You've got nested if, and you need them.
8:37 am on Feb 25, 2014 (gmt 0)

Junior Member

joined:Jan 9, 2014
posts: 150
votes: 0


Hmph. I've been using PHP for about 6 years (Perl for almost 20), and never seen this condition. But you're right; I've found this problem defined as a "dangling else" problem.

I actually have another script that has been using almost the exact same if..else() statement for years(with nested if() statements, without braces), and it's working as I expected, so I figured there had to just be a typo or something.

For anyone else reading this in the future, the logic is that PHP doesn't know whether the else if() is in response to the inner or outer if(). So, it's reading it in a way I didn't expect:

// I'm adding .. to show indention
$approved = false;

if ($default == "alpha")
. . if ($sttMod >= strtotime("-30 days")) $approved = true;
. . else if ($default == "beta" || $default == "charlie")
. . . . if ($sttMod >= strtotime("-90 days")) $approved = true;
. . . . else $approved = true;

Now, why it works in one script and not the other, I dunno. I'm guessing that it working in the other script is more or less an accident that just happens to give the same result.
12:01 pm on Feb 25, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


...working in the other script is more or less an accident


This is quite probable.

Many coding standards (not just PHP) stipulate you should always use braces to enclose your statement blocks - and for good reason - it avoids these kind of "hard to find" runtime errors. Contrary to your question title, this is not a "syntax" error - if it was, it would be a lot easier to find since an error would be generated.

Also note that in PHP it is preferable to write the "else if" construct as "elseif" (one word). Whilst the two syntaxes will work the same in your code above (essentially curly brace syntax), attempting to use "else if" (two words) in the alternative (colon) syntax is invalid.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members