Reading commandline input in PHP

In PHP, all commandline parameter are passed to the $argv array. Following example shows how to use this array:

  $op1 = $argv[1];
  $op2 = $argv[2];
  $sum = $op1 + $op2;
  print 'Sum: ' . $sum . "\n";

$argv[1] and $argv[2] contain the passed parameters in the order they were typed. The last line prints a dump of the data where the first parameter is the name of the file.

$ php sum.php 2 3
Sum: 5
   [0] => sum.php
   [1] => 2
   [2] => 3

is_dir not working

The is_dir() function in PHP allows users to check whether a file is a directory. It returns true if the file is a directory. However, the addresses provided must be RELATIVE TO THE CURRENT DIRECTORY. Suppose we have a directory structure as follows:

|-- dir1
  |-- dir2
     |-- dir3

where cwd is the current working directory

is_dir(dir1);  // returns true
is_dir(dir2);  // returns false
is_dir(dir1/dir2);  // returns true

Displaying multiple fields in a single line in drupal7 views

Suppose you have three fields (Title, field_city, field_country) to display in your view. By default they will display in three separate lines as follows:

Benjamin Franklin

To display this information in a single line, you need to edit the third field. Expand Rewrite Results, then type in the following:

[Title], [field_city], [field_country]

This will result in the following:

Benjamin Franklin, Philadelphia, USA 

Fatal error: Allowed memory size exhausted

Drupal can be very memory intensive CMS, especially if you have lots of menus, blocks and modules. If you exhaust the allowed memory size, you would get an error such as the following

Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 41 bytes) in somefile

To fix this problem, you need to allocate more memory.

1. Open you php.ini file If you don't know where this file is, run locate php.ini. Mine is located at /etc/php.ini. Make sure you back up this file before editing it.

Removing quotes - a step by step example of manipulating array data

This article shows some techniques of manipulating data in an array. To keep it simple, let's start by looking how we can remove quotes from a variable. To remove a character, we can replace it with nothing using str_replace() function.

$a = "'pineapple'";
$b = '"guava"';
print "$a $b\n";
$a = str_replace("'","",$a);
$b = str_replace('"','',$b);
print "$a $b\n";

This results in:

'pineapple' "guava"
pineapple guava

Consider this code

Quick way to find which log file is logging errors

On development systems it is common practice to have different log file to log PHP error logs for different tools and different versions of the same tool. Generally it is a good idea to use meaningful names to make it easy to distinguish between the log files. If you have doubts, you can either look inside the code to see which error log file is being called or do the following:

$ cd ~
$ ls -al /var/log/httpd before.diff

Run the tool such that it would write something to the log file. Then

Parsing a sentence in PHP

A sentence can be parsed in many different ways in PHP. A few methods are presented here along with their analysis. Regardless of the language you use for parsing a sentence, you can either match the characters of interest, or match the characters you wish to exclude and split on them or you can use a ready-made function if one is available.

Consider the following code:

$sentence = "Hey, Carol's 18th birthday is 15 days from today.";
$words = preg_split('/\W+/', $sentence);


PHP date arithmetic

strtotime() function can be used for date arithmetic. Following are some examples:

Get next date

print date('Y-m-d', strtotime('+1 day', strtotime('2013-09-30')));



Get previous month

print date('Y-m-d', strtotime('-1 month', strtotime('2013-09-30')));



Go back 5 years

print date('Y-m-d', strtotime('-5 year', strtotime('2013-09-30')));