Rumah > pembangunan bahagian belakang > tutorial php > PHP性能分析(001)-str_repeat ($input, $multiplier)

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

WBOY
Lepaskan: 2016-08-08 09:29:25
asal
1043 orang telah melayarinya

<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> $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> $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> 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></code>
Salin selepas log masuk

以上就介绍了PHP性能分析(001)-str_repeat ($input, $multiplier),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan