Category Archives: PHP

Removing UTF8 BOM from PHP files

UTF8 Byte Order Mark (BOM) is sequence of bytes for represent the file as UTF8. The sequence is hexadecimal EF BB BF value also you can see it at the beginning of the file as ï»¿. When these symbols is used the text editor recognizes that file as UTF8.

In most cases it creates a problem for PHP programmers. When php file is interpreted these symbols sent to the output directly (because there are not inside of <?php ?> tags). If the php file modifies or sends headers, BOM is sent before the headers(as an output) and sending headers fails. Usually you see such error:

Warning: Cannot modify header information – headers already sent by (output started at /path/to/php/public_html/config.php:28) in /path/to/php/public_html/index.php on line 101

The solution is to remove the UTF8 BOM signature from the PHP files. On Notepad++ or EmEditor you can save the file without UTF8 BOM. Another method is to open the files with the editor with can’t read utf8 and remove these byte order manually.

The easiest way is bomremover tool. You can get it from http://code.google.com/p/bomremover/ .

 

#cd /path/to/php/files/
#/path/to/bomremover/bomremover.sh -r

This will recursively locate the files and remove the BOM from files.

For removing BOM from a single file:

#cd /path/to/php/files/
#/path/to/bomremover/bomremover.sh index.php

 

Reference:

Getting twitter messages on PHP

It’s usual on web sites to place a widget with twitter messages by site owner. There are many methods to get messages from Twitter. For example you can do it using Twitter API.

There is another very simple method. It’s possible to get the messages via RSS.

The page contains RSS feed of tweets by me.

http://twitter.com/statuses/user_timeline/adilaliyev.rss



On PHP you can get the contents of RSS with a help of cURL:

<?php
// create a new cURL handle
$ch = curl_init();
 
// set RSS URL
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/adilaliyev.rss");
 
// set additional options
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
// get the contents
$a = curl_exec($ch);
 
echo ($a);
 
// close cURL handle
curl_close($ch);
?>

After getting the RSS feeds you can manipulate it using XML Features of PHP.

PHP 5.3 is released

PHP-5-3-mainI am happy to announce about PHP 5.3 release. Some of PHP 5.3 advantages are the followings:

namespaces (http://php.net/namespaces)
With namespace support grouping or packaging the classes will be easier and neat. As a Java and .NET lover I liked this feature very much.

anonymous functions (http://www.php.net/manual/en/functions.anonymous.php)
JavaScript fans will like it. It’s not too good idea to use anonymous functions everywhere, but, it’s very useful if write very short functions and you use only for one problem.

Late Static Bindings (http://php.net/lsb)
The feature for late binding of static methods.

Improvement on ternary operator (http://php.net/ternary)
You can leave out the middle part of ternary operator from now.  The expression cond1?:expression is valid while it was not in earlier versions of PHP.

Flexibility in php.ini (http://www.php.net/manual/en/ini.sections.php)
You can define the settings depending on the PATH or HOST where the script will run.

MySQL native drivers (http://php.net/mysqli.mysqlnd)
You don’t need to install MySQL Client for being able to work with remote MySQL. The PHP’s mysql driver is enough.

Hmmm. I will be waiting for Zend Framework and Zend Server optimized for PHP 5.3 :)