0

I am reading some WordPress plugin code, pardon my french, and even though I have coded a lot of stuff in anything from C to JavaScript, including PHP, I am not sure what kind of logic is accomplished with following:

<?php

    function dcontact_options_page() {

        if($_GET['action'] == 'edit_group') {
            dcontact_action_edit_group();
            return;
        }
        elseif($_GET['action'] == 'edit_form') {
            dcontact_action_form_settings();
            return;
        }
        elseif(isset($_POST['set_order'])) {
            $result = dcontact_action_set_order();
        }
        else if(isset($_POST['new_group'])) {
            $result = dcontact_action_new_group();
        }
        else if($_GET['action'] == 'delete_group') {
            $result = dcontact_action_delete_group();
        }

        if(!isset($result['message']) && isset($_GET['message'])) $result['message'] = urldecode($_GET['message'])
    ?>

    <div class="wrap">
        <div class="icon32" id="icon-options-general"><br></div>

        <!-- More HTML AND PHP code blocks -->

    <!-- And then suddenly... -->

<?php
    } /// What's this? End of function definition? PHP chokes on this with syntax error.

    ///...

?>

I am no stranger to mixing PHP and verbatim output, and I also did my fair share of entering and exiting PHP blocks in the middle of say if statements, but this tops it. Can anyone explain what the guy was trying to accomplish? Does the code imply that if the if condition evaluates to true then whole lot of markup is written out (and more PHP is executed), but then the function definition ends, suddenly? Maybe it's what the function does? I mean, I would have used a HEREDOC syntax for readability.

And to sum it up, my command line PHP 5.4.7 preprocessor chokes on the last }. And I can't say I blame it. Haven't seen spaghetti code like this in some time now.

5
  • 2
    Hmm that last } is for the function dcontact_options_page() end brace. Wow thats an ugly if set :P Commented Sep 25, 2012 at 10:02
  • Alright, that also crossed my mind. But then I still cannot comprehend what exactly the function does and why do it in such an ugly way (no other way?). My next bet is that he simply decided to do two bad things at once - split an if statement and a function body in multiple preprocessing directives. Commented Sep 25, 2012 at 10:04
  • A ; is missing after $result['message'] = urldecode($_GET['message']) Commented Sep 25, 2012 at 10:04
  • This is an ugly mix of PHP and HTML, but I've seen so much worse... Commented Sep 25, 2012 at 10:07
  • I'm seeing another problem there also: else if($_GET['action'] == 'delete_group') { shouldnt it be elseif($_GET['action'] == 'delete_group') { ? It is new to me that php likes else"blank"if. Commented Sep 25, 2012 at 10:14

2 Answers 2

2

This part:

if(!isset($result['message']) && isset($_GET['message'])) $result['message'] = urldecode($_GET['message'])

Is missing a semi-colon at the end. It would be much clearer if the original author had used proper bracing for single-statement conditionals, e.g.:

if(!isset($result['message']) && isset($_GET['message'])) {
    $result['message'] = urldecode($_GET['message'])
}

That would have put the error location just one line below the offending line :)

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, the person who wrote the rest of the code must really hate semicolons, maybe he had a low-baud connection to the workstation. There are no semicolons anywhere, save for some special places.
0

The if statement before the first closing PHP tag is a form of shorthand.

if(!isset($result['message']) && isset($_GET['message'])) $result['message']  = urldecode($_GET['message'])

Could be more clearly written as:

if (!isset($result['message']) && isset($_GET['message'])) {
  $result['message'] = urldecode($_GET['message']);
}

Also, the if statement should also be followed by a semicolon, but as it is followed by a closing PHP tag, is not strictly required.

2 Comments

Except that this what chokes PHP when it finds a } later on, right?
I don't find that it throws a syntax error in 5.3.8. Would need to know the syntax error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.