O'Reilly logo

PHP 5 Power Programming by Derick Rethans, Stig Sæther Bakken, Andi Gutmans

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

15.2. Quickstart

Instead of slowly explaining some of the building blocks of the scripting engine, this section dives into coding an extension, so do not worry if you don't see the whole picture right away.

Imagine you are writing a web site but need a function, which will repeat a string n times. Writing this in PHP is simple:

function self_concat($string, $n)
{
    $result = "";

    for ($i = 0; $i < $n; $i++) {
        $result .= $string;
    }
    return $result;
}

self_concat("One", 3) returns "OneOneOne".self_concat("One", 1) returns "One".

Imagine that for some odd reason, you need to call this function often, with very long strings and large values of n. This means that you'd have a huge amount of concatenation and memory reallocation going on in your script, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required