ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾PHP ±à³Ì£¾PHP 5 Power programming
Team LiB
Previous Section Next Section

13.8. Changes in Functions

Some minor changes in functions break backward compatibility. There are countless other additions to functions and additional functions, but these do not affect compatibility between PHP 4 and PHP 5.

13.8.1. array_merge()

This function no longer accepts a non-array parameter as one of its arguments. In PHP 4, it was perfectly valid to use scalar types, like an integer or string (but not a variable representing "null"), as parameter. These types are happily included as an element in the resulting array. PHP 5 no longer supports this. If you use a scalar type, PHP 5 issues an error of type E_WARNING and return an empty array. You can see this behavior by comparing the output of this script from PHP 4 and PHP 5:

<?php
    $array1 = array (1, 2, 3, 4);
    $array2 = null;
    $array3 = 'non-array';
    $array4 = array ('a', 'b', 'c');

    print_r(array_merge($array1, $array2, $array3, $array4));
?>

The output with PHP 4 is
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => non-array
    [5] => a
    [6] => b
    [7] => c
)

The output with PHP 5 is
Warning: array_merge(): Argument #2 is not an array in /13-making 
the-move/array_merge.php on line 7

Warning: array_merge(): Argument #3 is not an array in /13-making 
the-move/array_merge.php on line 7

13.8.2. strrpos() and strripos()

strrpos() and strripos() search for the last occurrence of a string inside a string in a respectively case-sensitive and case-insensitive way. In PHP 5, the full $needle is searched for in the string, searching from the end rather than the first character of this $needle string, as in PHP 4. The following example shows this:

<?php
    $str = "This is a short string.";


    var_dump(strrpos($str, "small"));
?>

In PHP 4, this returns position 16 (the index of the "s" of "string"):

int(16)

In PHP 5 this returns

bool(false)

It is possible that more functions broke compatibility between PHP 4 and PHP 5, but they are either not known, a bug fix, or are too unimportant to be noticed.

    Team LiB
    Previous Section Next Section