PHP5: More New Functions

Posted on March 4, 2011

Here are some new functions in PHP5 that don’t fit into any particular category.

Fun With HTTP Headers

Want to know what HTTP headers have been sent to the browser (or will be sent)? Now you can, using the headers_list() function. (You can still use headers_sent() to check if the headers have been sent yet.)

var_dump(headers_list());

/* Example output:
array(4) {
[0]=> string(23) "X-Powered-By: PHP/5.1.3"
[1]=> string(19) "Set-Cookie: foo=bar"
[2]=> string(24) "Content-type: text/html"
}
*/

You can also now use header_remove() to remove an individual header that was previously set using header().

PHP5 also has the get_headers() function for retrieving the HTTP response headers from any URL:

$url = 'http://www.example.com';
print_r(get_headers($url));

/* Example output:
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html
)
*/

All-in-One Functions

PHP5 finally adds file_put_contents(), a counterpart to file_get_contents(). This makes it handy for writing a string or other data to a file in one line, without needing a compatibility library.

PHP5 also provides an easy way to build a query string with the http_build_query() function. Just pass it an array or object (even nested arrays!) and it will return the properly-formatted query string. You can also specify the separators and encoding to use.

$data = array(
  'foo'=>'bar',
  'baz'=>'boom',
  'cow'=>'milk',
  'php'=>'hypertext processor'
);
echo http_build_query($data);

/* Output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
*/

The scandir() function has been added to the collection of directory functions. It returns an array of the names of files and directories inside a given directory. This is another time-saver that previously would have been done using opendir()+readdir().

Utilities

A new function called php_strip_whitespace() takes a PHP file and removes any comments and linebreaks, returning the changed code in a string. The manual explains this could be used for counting the actual lines of code. I suppose it could also be a first step in minification, though there are better tools out there. Because of PHP’s syntax, this function could also be used on CSS and JS files.

PHP4 lets you create temporary files using an unspecified filename in a system folder or using a specific filename in a given folder, but there’s no way to specify a filename in the system folder. (You can assume that the system folder is “/tmp”, but there’s no guarantee.) PHP5 now has a function called sys_get_temp_dir() that will tell you exactly what the system folder is, so now you can specify a filename and return to it later:

$temp_filename = tempnam(sys_get_temp_dir(), 'Tux');

Finally, if for some reason you want to know what the last (non-fatal) error was, you can call error_get_last() to get an array with that information.

New Parameters

PHP5 adds new parameters to a few functions.

The htmlentities() and htmlspecialchars() now take a double_encode parameter. If set to true, the functions will not encode existing HTML entities. Useful to prevent double encoding, but hopefully you already know whether a string has been encoded or not.

The functions for setting cookies — setcookie(), setrawcookie(), and session_set_cookie_params() — now take an httponly parameter. If set to true, the cookie will not be accessible through Javascript, protecting the site from cross-site scripting attacks.

Leave a Reply

  1.  

    |