Validating Email in PHP

Validating email is more difficult than one might think. Recently I found this this code at and I feel a need to republish this to emphasize its effectiveness and simplicity.

$email = "";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo "E-mail is not valid";
} else {
  echo "E-mail is valid";

PHP Array functions

Passing arrays by value and reference

The difference between passing by value and passing by reference is that you when pass by value, you are passing a copy of the array. When passing by reference, you are passing a pointer i.e any changes made in the function you pass to can modify the original.

function foo($var) {
function bar(&$var) {

print "Passed by value:\n";
print "Passed by reference:\n";


PHP File functions

Opening directory and listing files

See the following code:

  $dir = 'mydir';
  $myfiles = scandir($dir);


  [0] => .
  [1] => ..
  [2] => one.txt
  [3] => two.txt

To remove certain file name from the list, see the following code:

Installing PHP on Mac OS

Installing PHP is more than just installing the PHP source code on the system. Several libraries are also required to connect to databases, connect to GD, support XML, get LDAP access and so on. All these have to be installed manually on the machine. You can go through the painful process of manual installation and configuration or simply download and install a ready-made package from

Unless you have very specific needs, you should use this package.

PHP fatal error - call to undefined function mb_string

This error tells us that the mb_string library is not installed. mb_String mb_String is a library, which provides support for UTF-8 and UCS-2 characters. PHP uses ASCII by default which is a 256 character coding system. This means that it uses 8 bits to encode English alphabet and commonly used symbols such as ,.?;:"+-$#@ and many more. However, it does not code for foreign language characters such as é, ç, â, è, ñ, ü, and many others. mb_string provides double byte, i.e. 2 x 8bits to code for a character.

Encrypting and decrypting in PHP

The crypt() function provide one-way encryption. Using one-way encryption is like using a key to lock and unlock something. The key is your password.

To encrypt:

$pass = 'secret';
$encrypted = crypt($pass);
// $encrypted = $1$zaxz8vXb$.lZaoK40w/EtrkkogORYo0

To decrypt:

// entered password
$pass = 'secret';
// $password is the encrypted password
if (crypt($pass, $encrypted) == $encrypted) {
  print 'welcome';
} else {
  print 'wrong password';

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

Converting primitive wrapper objects to String

The following example shows how to convert objects of primitive wrapper classes to String.

public class PrimitiveWrapperToString {

   public static void main(String[] args) {
     Integer i = new Integer(1);
     Double d = new Double(1.1);
     Float f = new Float(2.2);
     Long l = new Long(13);

     String si = Integer.toString(i);
     String sd = Double.toString(d);
     String sf = Float.toString(f);
     String sl = Long.toString(l);

     System.out.println(si + " " + sd + " " + sf + " " + sl);

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