Improved Array-HandlingPHP

PHP  offers a plethora of new functions for accessing and manipulating arrays. These functions include, but are not limited to, array_splice(), array_push(), array_pop(), and array_diff().

Integrated Sessions  
One of the biggest problems with PHP 3 was its lack of an integrated session management system. Users either had to write their own sessions programs or rely on external session management systems, such as PHPLIB's session-handling functions. With the advent of PHP 4, the PHP Group authored a new sessions extension that allows for integrated sessions support.

Java Integration

PHP gives you the ability to integrate PHP with Java libraries and servlets. Simply create a new Java object and then you can call the Java object's methods and access its properties.

Buffering


PHP 4 offers an output-buffering subsystem that enables you access to the output of the script, manipulate it, and then do whatever you desire with the resulting data. One possible use of this feature is to create a page-caching system.

New Extensions

PHP  comes with many new extensions that weren't available with PHP 3. These extensions include, but are not limited to, the swf, curl, exif, cybercash, sockets, and ingres_ii extensions.

PEAR

PEAR stands for PHP Extension and Application Repository. The concept of PEAR is akin to that of Perl's CPAN—it is a repository of PHP classes and supporting extensions to help you program. For example, the File_Find class is distributed through PEAR. This class enables you to map and search different directory trees. More information about PEAR is available at http://pear.php.net/.


PHP's built-in substr() and substr_replace() functions, which enable you to read and write parts of the string:
<?php
$sub_str = substr ($str, $initial_pos);
$sub_str = substr ($str, $initial_pos, $str_len);

$new_str = substr_replace ($str, $replacement, $initial_pos);
$new_str = substr_replace ($str, $replacement, $initial_pos, $str_len);
?> 
 

PHP treats strings as a basic data type instead of an array of bytes. Therefore, it is possible for you to use a function such as substr() or substr_replace() to access and modify individual characters or portions of strings.

The first argument to substr() is the string on which you want to operate. The second argument to substr() specifies the beginning index of the substring you want to access. If the second argument is positive, the substring will start at that character counting from the beginning of the string. If the second argument is negative, the substring will start at that character counting from the end of the string.

Ternary Operator
 
 
the ?: conditional to test the value of the user input:
<?php

// If the user has provided a first argument to the program use
// that, otherwise STDIN (php://stdin)
$filename = isset ($argv[1]) ? $argv[1] : "php://stdin";

$fp = @fopen ($filename, 'r')
    or die ("Cannot Open $filename for reading");

while (!@feof ($fp)) {
    $line = @fgets ($fp, 1024);
    print $line;
}

@fclose ($fp);
?>

The preceding code implementing the ternary operator is the equivalent of the following:
<?php
if (isset ($argv[1])) {
    $filename = $argv[1];
} else {
    $filename = "php://stdin";
}
?>
However, PHP's ternary operator (?:) greatly reduces the time it takes for programmers to write these statements. Its syntax is as follows:
condition ? do_if_true : do_if_false;
 
The use of the ternary operator is what is known as "syntactical sugar"; that is, it is a construct that is not needed, but is a nice way of beautifying code. None the less, it can be used to replace ugly if .. else code blocks and improve the readability of your code.

The list() and array() constructs to switch the variables

 
the list() and array() constructs to switch the variables:
<?php
list ($var1, $var2) = array ($var2, $var1);
?>

In many other languages, you must use a temporary variable, like so:
<?php
$temp = $var1;
$var1 = $var2;
$var2 = $temp;
?>
However, in PHP, the list() construct does this for you. The list() construct is used to assign a list of variables

chr() and ord() or sprintf() to convert back and forth:
<?php
$letter = chr (67); // Upper case C
$ascii_code = ord ($letter); // 67

$letter = sprintf ("%c", $ascii_code); // Upper case C
?>

On the surface, converting ASCII values seems to be a pretty useless task. When I was a beginning programmer (Perl at the time), I thought that it was pointless for people even to write these functions and explanations. However, there are many cases in which you do need to convert back and forth.


To reverse all the words in a string, use a combination of the preg_split() function and the array_reverse() function:

<?php

function word_reverse ($str) {
    return implode ("", array_reverse (preg_split ("/\ s+/", $str)));
}

$str = "A rose by any other name";
$str_reversed = word_reverse ($str);
print $str_reversed;
// Outputs: name other any by rose A
?>
To reverse all the characters in a string, you can use PHP's strrev() function:
<?php

$str = "A rose by any other name";
$chars_reversed = strrev ($str);
print $chars_reversed;
// Outputs: eman rehto yna yb esor A
?> 
 
The word_reverse() function uses the array_reverse() function, which is available 
only with PHP.