Wednesday, August 5, 2009

strtok in PHP - a common mistake

strtok, that splits a string into tokens is a very useful function for the programmers. Here is an example code that shows the use of strtok function to extract integers from a comma separated string.

$list = "1,2,4,10,8,0,20,30,9";

$token = strtok($list, ",");
while($token)
{
print $token."\n";
$token = strtok(",");
}
?>

Using the code we get this output:

1
2
4
10
8

Where are the other four integers? Actually when the value of $token is zero (0) - the sixth integer in the list, while ($token) evaluates to False and the loop is broken there. Lets use while($token != FALSE) instead.

$token = strtok($list, ",");
while($token != FALSE)
{
print $token."\n";
$token = strtok(",");
}

Still the same output. Oh, the mistake is we used the '!=' operator which tests the equality between $token and FALSE. Here zero (0) is equal to FALSE. A stupid way to avoid the problem is to calculate the length - while(strlen($token)) and it will work definitely! as the length of the string '0' is 1. But best solution (to me) is to use the '!==' operator which means not identical. So the following code works fine:

$list = "1,2,4,10,8,0,20,30,9";

$token = strtok($list, ",");
while($token !== FALSE)
{
print $token."\n";
$token = strtok(",");
}
?>

Output:

1
2
4
10
8
0
20
30
9

It's always useful to know details about operators in PHP.

Tuesday, May 12, 2009

Trouble with Facebook Client API

I have working on facebook application development (it's a game named Fighter Jets), and facing some weired problem. I have decided to share my experience here.

Today I tried the post link feature of the facebook API. First I tried the following code:
$facebook->api_client->links_post($user_id, 'http://khaaan.com/','Best. Website. Ever.');
I got it from their API documentation.

I got error using it. It says that 'The url you supplied is invalid'. May be it's invalid. Then I changed the URL to http://www.google.com, which is valid definitely! Still getting the same problem!! WTF!

Then I searched their forum and found that I am not the only victim, some people already faced this problem and one of them solved the problem in the following way:
$facebook->api_client->links_post('http://khaaan.com/','Best. Website. Ever.', $user_id);

Which works! Needed to use the user id as the third parameter, but from the example in the API documentation we see that it's the first parameter.

I really don't understand why the documentation is wrong.

Wednesday, November 12, 2008

Forum for freelance software developers

I got a new forum for freelancers. They call it FreelanceFest. You can go and post your problems there. Hopefully someone will answer soon. There are language specific categories and also freelance site based categories available.

Monday, August 11, 2008

Convert FLV to MP3 using Perl

Few days back, I found an interesting Perl module that converts FLV file to MP3. Here is the code:

use FLV::ToMP3;

$flv_filename = "/home/video/music.flv";
$mp3_filename = "test.mp3";

my $converter = FLV::ToMP3->new();
$converter->parse_flv($flv_filename);
$converter->save($mp3_filename);

Visit the following links for details:

Send email using PHP

Sending emails using a PHP script / program is very easy. Just use the mail function: mail($to, $subject, $body);

Check mail function for details.

There can be some problems if it's not configured properly. Check the installation / configuration page for details.

Install Perl modules using cpan

Installation of Perl modules is very simple. Just use the command in the terminal:
cpan -install ModuleName

For example, to install LWP module, use the command:
cpan -install LWP

To install Mail::Sendmail module, use the command:
cpan -install Mail::Sendmail

Saturday, August 9, 2008

Perl - return value of a function

Can you guess the output of the following Perl code?

$sum = add(3, 5);
print $sum."\n";

sub add
{
$a = shift;
$b = shift;

return ($a + $b);
}

Yes, it prints 8.

Now what about the following code?

$sum = add(3, 5);
print $sum."\n";

sub add
{
$a = shift;
$b = shift;
}

hmm... what should it print? It prints 5 ! A garbage value?
Not actually. I have found that in Perl if you don't return anything, it returns the value of the last variable in the function.

Now you can tell the output of the following code :-)

$sum = add(3, 5);
print $sum."\n";

sub add
{
$a = shift;
$b = shift;

$c = 10;
}

yes, it's 10 !