如何在没有arg输入的情况下修改函数内的一个条件?

时间:2012-05-30 00:11:37

标签: php function

我需要调用一个输出html的函数。在这个函数中,有一个条件要从html中排除一行,我需要这一行。这个条件不是函数的参数。它是硬编码的。所以,我必须编写自己的代码,或者,有没有办法修改这个条件?

function get_media_item( $attachment_id, $args = null ) {
//....lots stuffs, then, arrive this part:
    $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
    $order = '';

    foreach ( $form_fields as $key => $val ) {
        if ( 'menu_order' == $key ) {
            if ( $gallery )
                $order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ). "' /></div>";
            else
                $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";

            unset( $form_fields['menu_order'] );
            break;
        }
    }
//... other stuffs
}

我需要在其他标签中调用此功能,而不是“图库”标签。所以,我无法获得$ order输入框。

1 个答案:

答案 0 :(得分:0)

如果您不想将参数传递给函数(建议使用btw)来修改条件,则可以使用globalize变量(或会话)。

示例:

function test(){
   global $instructions;

   if (isset($instructions['fetch'])){
      // new codes
   } else {
      // existing codes
   }

   return $result;
}

// implement:

$instructions = array();
$instructions['fetch'] = TRUE;
echo test();