Chapter 14. Performance Tuning

Dynamically interpreted languages, such as PHP, are known for their flexibility and ease of use but not necessarily for their speed. This is partly because of the way their type systems work. When types are inferred at runtime, it’s impossible for the parser to know exactly how to perform a certain operation until it’s provided with data.

Consider the following, loosely typed PHP function to add two items together:

function add($a, $b)
{
    return $a + $b;
}

Since this function does not declare the types for the variables passed in or the return type, it can exhibit multiple signatures. All of the method signatures in Example 14-1 are equally valid ways to call the preceding function.

Example 14-1. Various signatures for the same function definition
add(int $a,    int $b):   int 1
add(float $a,  float $b): float 2
add(int $a,    float $b): float 3
add(float $a,  int $b):   float 4
add(string $a, int $b):   int 5
add(string $a, int $b):   float 

add(1, 2) returns int(3)

add(1., ...

Get PHP Cookbook 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.