if problems

This is definitely one for the geek files, so if multi-condition PHP if statements mean nothing to you, feel free to move along.  I won’t be offended.

Ok, just geeks left?  Good, here we go.

Last night I was trying to correct a syntax error and came across a helpful way to determine if all the parentheses in my if statement matched each other.  Basically how it works is like this.  Start counting each parenthesis in your statement. Each time you see an open parenthesis you increase your count by 1 and each time you see a close parenthesis you decrease your count by 1.  Let me illustrate:

// 12                 1    23   4           3      2    3      4                3210
if (($condition1 != $a) || ((len($condition2) == $b) && (!isset($_POST['submit'])))) {

Don’t know if that helps you or not, but as the editor i was using (online) didn’t have a syntax checker, it helped me a great deal.

Share

12 Comments

PeterAugust 27th, 2009 at 9:29 am

Or you could just use an editor with parenthesis and bracket highlighting like I do… (Notepad++)

DuncanAugust 27th, 2009 at 9:34 am

That’s so painfully obvious that I never thought of it! Good algorithm.

I think it wouldn’t be too hard to construct a Ubiquity command that runs this test on a selected line of code (I’ll maybe post some exploratory code up on github in a while.

Thinking out loud: Can we use a similar approach (say, counting backwards?) to find the un-closed parenthesis? If so, then we could build that into the command too and make a pretty awesome little syntax checker…

DuncanAugust 27th, 2009 at 10:29 am

I just did some preliminary checking, and found one case that breaks this algorithm. If you’re missing an equal number of open and closed parentheses, then it doesn’t detect an error.

Missing one ) and one ( case:
12 * *3 4 3 2 3 4 3210 !UNDETECTABLE ERROR
01 * *2 3 4 3 2 3 4321 !UNDETECTABLE ERROR
if (($condition1 != $a || (len($condition2) == $b) && (!isset($_POST[‘submit’])))) {

DuncanAugust 27th, 2009 at 10:31 am

I just threw my test cases on github… this is enough for now, but this is still interesting to explore.
http://gist.github.com/176329

andyAugust 27th, 2009 at 11:06 am

@peter yes, but notice i was using an online editor that didn’t have a syntax checker…
@duncan hmm, that’s true about the case that breaks that algorithm, but i’d be interested in seeing that ubiquity plugin. I’ll see if i can tweak something to use the pre tag…

PeterAugust 27th, 2009 at 11:10 am

I realize this is purely theoretical, but you also have 6 too many parenthesis. You could accomplish the same logic by not wrapping every condition in a parenthesis. Like thus:

if ($condition1 != $a || (len($condition2) == $b && !isset($_POST[‘submit’])))

andyAugust 27th, 2009 at 11:34 am

@peter are you sure about that? doesn’t that open the possibility, for example, that $condition1 != $a OR the len of $condition2, etc. rather than that $condition1 != $a OR that the len of $condition2 == $b.

Eg. with
$condition1 = ‘foo’
$condition2 = ‘bar’
$a = 1
$b = 2
‘foo’ != 1 OR 3 rather than ‘foo’ != 1 OR 3 == 2

DuncanAugust 27th, 2009 at 11:48 am

Here’s a quick mock-up Ubiq command. This only does the left-to-right check. The right-to-left check should be pretty easy to implement by getting the array length and then looping down from arrayLength-1 to zero.

Also, as I predicted, it didn’t detect an error on the exceptional case.

http://gist.github.com/176391

DuncanAugust 27th, 2009 at 12:56 pm

Now that I think about it, the “exceptional case” would compile just fine… but it would produce a logic error.

I guess that’s beyond the scope of this algorithm then, so we can’t really say that this case “breaks” the algorithm.

I’m sure more extensive use case analysis would produce other things to think about.

MomAugust 29th, 2009 at 9:33 am

Oh, my! I should have taken your advice and moved on along! I have no earthly idea why counting parentheses would be fascinating to you! 🙂

andyAugust 29th, 2009 at 5:19 pm

@Mom, That’s ok, i warned you. 😛

ScottSeptember 23rd, 2009 at 10:55 am

Mrs. Anglea, I work in web design, and I’m really not sure either… 😛

Leave a comment

Your comment