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 entropy.ch.

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:

<?php
  $op1 = $argv[1];
  $op2 = $argv[2];
  $sum = $op1 + $op2;
  print 'Sum: ' . $sum . "\n";
  print_r($argv);
?>

$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
Array
(
   [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:

cwd
|-- 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

Java stack

Stack is a LIFO ADT (Last in first out abstract data type) and a linear data structure. In a jar the items on top (i.e. items that went in last) come our first. In a LIFO ADT, items that go in last come our first.

Stacks are a very useful data structure. Although you have the possibility of creating your own stack data structure from Java code, it is a better idea to simply use the stack provided by Java.

Code Following code shows how to use Java's stack:

Pages