PHP效能分析(001)-str_repeat ($input, $multiplier)

WBOY
發布: 2016-08-08 09:29:25
原創
1012 人瀏覽過

<code><span><span><span><?php</span>
header(<span>"Pramga: no-cache"</span>);

<span>/**
 * 获得空心金字塔:<br/>
 * 1.使用了memcpy和memmove,效率最高。
 *
 *<span> @param</span> number $levelNum
 *            金字塔层数:默认20
 *<span> @return</span> string 金字塔的结果
 */</span><span><span>function</span><span>getEmptyJinZiTa</span><span>(<span>$levelNum</span> = <span>20</span>)</span>
{</span><span>$SPACE</span> = <span>" "</span>; <span>// 用于填充空格</span><span>$STAR</span> = <span>"*"</span>; <span>// 用于填充型号</span><span>$decreseSpace</span> = <span>$levelNum</span> - <span>1</span>; <span>// 第一行开始的空格,逐行递减</span><span>$result</span> = <span>""</span>;
    <span>for</span> (<span>$level</span> = <span>1</span>; <span>$level</span> <= <span>$levelNum</span>; ++ <span>$level</span>) { <span>// 循环外层数</span><span>$result</span> .= str_repeat(<span>$SPACE</span>, <span>$decreseSpace</span> --);
        <span>if</span> (<span>$level</span> >= <span>1</span> && <span>$level</span> < <span>$levelNum</span>) {
            <span>$result</span> .= <span>$STAR</span>;
            <span>$spaceNum</span> = (<span>$level</span> - <span>1</span>) * <span>2</span> - <span>1</span>;
            <span>$result</span> .= str_repeat(<span>$SPACE</span>, <span>$spaceNum</span> < <span>0</span> ? <span>0</span> : <span>$spaceNum</span>);
            <span>if</span> (<span>$level</span> != <span>1</span>) <span>// 第一行就一个*</span><span>$result</span> .= <span>$STAR</span>;
        } <span>else</span><span>if</span> (<span>$level</span> == <span>$levelNum</span>) { <span>// 最底层需要填充全*</span><span>$result</span> .= str_repeat(<span>$STAR</span>, <span>$level</span> * <span>2</span> - <span>1</span>);
            }
        <span>$result</span> .= <span>"<br/>"</span>;
    }
    <span>return</span><span>$result</span>;
}

<span>echo</span> getEmptyJinZiTa();<span>//打印金字塔重点内容</span><span>?></span></span></code>
登入後複製

以上就介紹了PHP效能分析(001)-str_repeat ($input, $multiplier),包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板