将参数传递给php中的函数

I have some php code that is pretty much being duplicated save for some minor variable naming differences. How can I turn this into a reusable function where I can pass arguments thru?

This is the code which I am using twice. The second one is the same except all references of "affiliate" is changed to "social".

<?php
    $affiliate = wp_list_bookmarks( array( 'categorize' => 0, 'category' => '7', 'title_li' => '', 'orderby' => 'rating', 'show_images' => 0, 'echo' => 0 ) );
    preg_match_all( '/<li>.*?<\/li>/', $affiliate, $affiliate_matches );
    foreach ( $affiliate_matches[0] as $affiliate_match ) {
        preg_match( '/title=".*?"/', $affiliate_match, $affiliate_title );
        echo str_replace(
            $affiliate_title[0],
            $affiliate_title[0] . ' ' . strtolower( str_replace( array( 'title="', ' ' ), array( 'class="', '-' ), $affiliate_title[0] ) ),
            $affiliate_match
        ) . "
";
    }
?>

The other one is:

<?php
    $social = wp_list_bookmarks( array( 'categorize' => 0, 'category' => '2', 'title_li' => '', 'orderby' => 'rating', 'show_images' => 0, 'echo' => 0 ) );
    preg_match_all( '/<li>.*?<\/li>/', $social, $social_matches );
    foreach ( $social_matches[0] as $social_match ) {
        preg_match( '/title=".*?"/', $social_match, $social_title );
        echo str_replace(
            $social_title[0],
            $social_title[0] . ' ' . strtolower( str_replace( array( 'title="', ' ' ), array( 'class="', '-' ), $social_title[0] ) ),
            $social_match
        ) . "
";
    }
?>

I was thinking maybe I can call the function like

<?php links( array( 'affiliate', 7 ) ); ?>

or

<?php links( array( 'social', 2 ) ); ?>


Would combining them into a reusable function save processing time/resources or would it not matter?

2个回答

the only thing that really changes is the category id so you only need to pass this to the function.

function links($categoryId) {
        $affiliate = wp_list_bookmarks( array( 'categorize' => 0, 'category' => $categoryId, 'title_li' => '', 'orderby' => 'rating', 'show_images' => 0, 'echo' => 0 ) );
        preg_match_all( '/<li>.*?<\/li>/', $affiliate, $affiliate_matches );
        foreach ( $affiliate_matches[0] as $affiliate_match ) {
            preg_match( '/title=".*?"/', $affiliate_match, $affiliate_title );
            echo str_replace(
                $affiliate_title[0],
                $affiliate_title[0] . ' ' . strtolower( str_replace( array( 'title="', ' ' ), array( 'class="', '-' ), $affiliate_title[0] ) ),
                $affiliate_match
            ) . "
";
        }

    }
doushao1948
doushao1948 嘿..愚蠢的我。 我忽略了这一点。 我想是时候睡觉了。 太糟糕了我现在无法删除帖子。
接近 9 年之前 回复



它不会节省任何计算机时间,它节省的是你的时间,不需要维护代码两次。 (但请注意,将其转换为函数并不会让您花费更多精力而不仅仅是两次。)</ p>

此外,我认为没有理由将“社交”一词传递给 该功能 - 它实际上从未在任何地方使用过。</ p>
</ div>

展开原文

原文

It would not save any computer time, what it saves is your time, by not having to maintain code twice. (But watch out that converting it into a function does not cause you to spend more effort than just having it twice.)

Also, I see no reason to pass the word 'social' to the function - it's never actually used anywhere.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问