dongzhenshen7435 2012-09-25 10:00
浏览 33
已采纳

以下PHP代码做了什么(它混合了PHP和输出)?

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.

  • 写回答

2条回答 默认 最新

  • dongpin4611 2012-09-25 10:04
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题