homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Syntax error, hoping for a second set of eyes
GoNC




msg:4648829
 6:20 am on Feb 25, 2014 (gmt 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?

 

Habtom




msg:4648831
 6:37 am on Feb 25, 2014 (gmt 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.

GoNC




msg:4648847
 8:37 am on Feb 25, 2014 (gmt 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.

penders




msg:4648874
 12:01 pm on Feb 25, 2014 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved