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.

4

2 回答 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 :)

于 2012-09-25T10:04:04.627 回答
0

第一个结束 PHP 标记之前的 if 语句是一种简写形式。

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

可以更清楚地写成:

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

此外,if 语句后面也应该跟一个分号,但是因为它后面跟一个 PHP 结束标签,所以并不是严格要求的。

于 2012-09-25T10:10:23.333 回答