Armen Michaeli
Armen Michaeli

Reputation: 9140

What does the following PHP code do (it mixes PHP and output)?

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.

Upvotes: 0

Views: 166

Answers (2)

scottlimmer
scottlimmer

Reputation: 2268

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.

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173562

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 :)

Upvotes: 2

Related Questions