Difference between equality(==) and identity(===) in PHP

Double equals(==) compares the values of variables for equality, type casting as necessary. Triple equals(===) checks if 2 variables are of the same TYPE and have the same value.
A full explanation of the differences are available in PHP Manual
Examples:
1 === 1: true
1 == 1: true
1 === “1”: false //1 is an integer, “1” is a string
1 == “1”: true //“1” gets casted to an integer, which is 1
“foo” === “foo”: true //both operands are strings and have the same value
Note: two instances of the same class do NOT match the === operator
$a = new stdClass();
$a->foo = “bar”;
$b = clone $a;
var_dump($a === $b); //bool(false)

PHP Composer

What is PHP Composer?

It’s a very good dependency management tool for PHP. It manages all the libraries that you have on your project.

Advantage

The composer will pull all the required libraries, dependencies and keep track all of them just in individual project not globally. Furthermore, you might be aware of PEAR which is established as PHP package manager. However, it has been abandoned for several reasons. First of all, much of the code in PEAR is out of date. Secondly, PEAR forces you to install packages system wide rather than on a project-by-project.

Installation

Installing composer is quite easy. In this tutorial, I’ll use curl to install composer thus make sure you have curl installed or enabled in your system. If not, use the following to install

sudo apt-get install php5 git php5-curl

Now move to your project location and run this command:

curl -sS https://getcomposer.org/installer | php

You should see a success message like this:

All settings correct for using Composer
Downloading...
Composer successfully installed to: /var/www/my_project/composer.phar
Use it: php composer.phar

Usage

Now that you have composer installed. To use the composer, it requires composer.json file in which describes the dependencies of your project. For instance, our project depends on PEAR, Mail, Mail_Mime, Net_SMTP, Service_JSON. So let’s create the file:

{
    "require": {
        "pear-pear.php.net/PEAR": "1.9.4", 
        "pear-pear.php.net/Mail": "1.2.0",
        "pear-pear.php.net/Mail_Mime": "1.8.5",
        "pear-pear.php.net/Net_SMTP": "1.6.1",
        "pear-pear.php.net/Services_JSON": "1.0.3"
        }
    }

One question rising from here is that where you can find the package and the version for this file. Here is a good place for you to do that.
Ok now you can run a command to install those dependencies on your project:

php composer.phar install

Autoloading

Once you have all your dependencies installed. You need to autoload them into your project. In your PHP project, simply specify this in your index or bootstrap file:

require 'vendor/autoload.php';

Conclusion

Composer is now widely used by many PHP developers. So it’s better for you to start using it today.

Persistance and Duplication jQuery cookie

Using jQuery cookies to maintain user states becomes more popular. However, i’ve recently found a problem where state didn’t appear to be persisting and on viewing the cookie information, using the web developer toolbar, i had multiple versions of the same cookie.

when a cookie is created, its path is set default to ‘/’ meaning that the cookie is valid throughout the whole domain

Cookies can also be set per directory and they’ll be valid for a certain set of pages.

Therefore, to overcome this problem, instead of a simple name, value pair:

$.cookie('the_cookie', 'the_value');

you need explicitly set the cookie’s path as "/"

$.cookie('the_cookie', 'the_value', { path: "/" });

PHP Unit Test – part 1

Prerequisites:

  1. PHPUnit Framework installed
  2. extends class PHPUnit_Framework_TestCase

phpunit

  • Test number
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testFailureNumber(){
        $this->assertEquals(1, 1);
    }
}
  • Test string
public function testFailureNumber(){
    $this->assertEquals(1, 1);
 }
  • Test xml
public function testFailureXml(){
    $expected = new DOMDocument;
    $expected->loadXML('<foo><bar/></foo>');
    $actual = new DOMDocument();
    $actual->loadXML('<foo><bar/></foo>');
    $this->assertEquals($expected, $actual);
 }
  • Test object
public function testFailureObject(){
    $expected = new stdClass();
    $expected->foo = 'foo';
    $expected->bar = 'bar';
    $actual = new stdClass();
    $actual->foo = 'foo';
    $actual->bar = 'bar';
    $this->assertEquals($expected, $actual);
 }