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, ...

Get PHP 5 Power Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.