PayPal Mini Cart does not display

It is quite easy to implement PayPal Mini Cart on to your web site but there is a simple error that can stop the cart from displaying on your web page.

When you create you web page, make sure that the web page has proper validation at the top of the page such as

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>

Secondly, the Javascript code should be poistioned at the bottom of the web page just above the </body> tag.

Differences between PHP4 and PHP5

This listing of the differences between PHP4 and PHP 5 is is probably something that is quite common, but it is always worth a review.

PHP5 is a lot different than PHP4 with the main differences being in how it handles objects and classes.

Here are 10 major differences between PHP4 and PHP5 that you need to know:

1. Unified Constructors and Destructors:

In PHP4, constructors had same name as the class name. This used to cause overhead because every time you changed the class name, you had to change all the occurrences of that name.

In PHP5, you simply need to name your constructors as __construct(). (the word ‘construct’ prefixed by double underscores). Similarly you can name your destructors as __destruct(). (the word ‘destruct’ prefixed by double underscores.) In destructors, you can write code that will get executed when the object is destroyed.

2. Abstract Class:

PHP5 lets you declare a class as ‘Abstract’. (i.e. a class whose object cannot be created. You can only extend an abstract class) Also, a class must be defined as abstract if it contains any abstract methods. And those abstract methods must be defined within the class which extend that abstract class. You can include complete method definitions within the abstract methods of abstract class.

3. Final Keyword:

PHP5 allows you to declare a class or method as ‘Final’ now. You just need to use ‘final’ keyword that will indicate that the class cannot be inherited or the method cannot be overridden.

4. Exception Handling:

PHP5 has introduced ‘exceptions’. An exception is simply a kind of error and the ‘exception error’ can be handled in an exception object. By using an exception, one can gain more control over the simple trigger_error notices we were stuck with before.

When you are about to perform something ‘risky’ in your code, you can surround your code with a ‘try…catch’ block. First you surround your code in a ‘try {…….}’ block, then if an exception is thrown, your following ‘catch{……}’ block is there to intercept the error and handle it accordingly. You can write some PHP code in your ‘catch’ block which will get executed when an error occurs in the ‘try’ block. If there is no ‘catch’ block, a fatal error occurs.

5. E_STRICT Error Level:

PHP5 introduces new error level defined as ‘E_STRICT’ (value 2048). This error levels notifies you when you use depreciated PHP code. It is not included in E_ALL, if you wish to use this new level you must specify it explicitly.

6. Autoloading (the __autoload() function):

PHP5 introduces a special function called ‘__autoload()’ (the word ‘autoload’ prefixed by double underscores). This function allows you to avoid writing a long list of includes at the top of your script by defining them inside this function. So you can automatically load object files when PHP encounters a class that hasn’t been defined yet.

Example:

function __autoload ($class_name) {

include $class_name . ‘.php’;

}

7. Visibility:

In PHP5, class methods and properties now have ‘visibility’. There are 3 levels of visibilities:

Public: ‘Public’ is the most visible. Methods are accessible to everyone including objects outside the classes. And properties readable and writable by everyone including objects outside the classes.
Private: ‘Private’ makes class members only available to the class itself.
Protected: ‘Protected’ makes class members accessible to the class itself and any inherited class (subclass) as well as any parent classes.

PHP4′s method of declaring a variable as ‘var’ keyword is still supported in PHP5. The ‘var’ keyword is now a synonym for the ‘public’ keyword now.

8. Pass by Reference:

In PHP4, everything was passed by value, including objects. Whereas in PHP5, all objects are passed by reference. Take a look at this PHP4 code for example –

$peter = new Person();
$peter->sex = ’male’;

$maria = $peter;
$maria->sex = ’female’;

echo $peter->sex; // This will output ‘female’

As you can see in the code above, if you wanted to duplicate an object in PHP4, you simply copied it by assigning it to another variable (Pass by value). But now in PHP5 you must use the new ‘clone’ keyword. So the above PHP4 code, will now look like this in PHP5 –

$peter = new Person();
$maria = new Person();

$peter->sex = ’male’;

$maria = clone $peter;
$maria->sex = ’female’;

echo $peter->sex; // This will output ‘female’

9. Interfaces:

PHP5 introduces ‘interfaces’ . An interface defines the methods a class must implement. All the methods defined in an interface must be public. An interface helps you design common APIs. It is not designed as a blueprint for classes, but just a way to standardize a common API. A big advantage of using interfaces is that a class can implement any number of interfaces. You can still only ‘extend’ on parent class, but you can ‘implement’ an unlimited number of interfaces.

10. New Functions:

PHP5 introduces new functions which are not found in PHP4. You can find the list of these new functions in the PHP manual.

Simple example of a class in PHP

A simple example of a class in PHP
<?php # person

class person {

public function __construct( $name, $age, $height, $weight )
{
$this->name   = $name;
$this->age    = $age;
$this->height = $height;
$this->weight = $weight;
}

// functions that return information

public function get_name()
{
return $this->name;
}

public function get_age()
{
return $this->age;
}

public function get_height()
{
return $this->height;
}

public function get_weight()
{
return $this->weight();
}

// functions that change the object.

public function change_name( $name )
{
$this->name = $name;
}

public function add_weight( $weight )
{
$this->weight = $this->weight + $weight;
}

public function lose_weight( $weight )
{
$this->weight = $this->weight – $weight;
}

}

//Initialise a person
$paul = new person(‘Paul Gibbs’, 17, ‘6\’1″‘, 210);

$john = new person(‘John Smith’, 36, ‘5\”, 160);

echo(“<br/>”);

print_r($paul);

echo(“<br/>”);

echo(“Loose 10 pounds from this person<br/>”);

$paul->lose_weight(10);

echo(“<br/>”);

print_r($paul);

echo(“<br/>”);

//A possible use :

$people = array($paul, $john);   //Create an array of people

foreach( $people as $person ) {
echo “This persons name is ” . $person->get_name() . “<br/>”;
echo “This person is “.$person->get_age().” years old.<br/>”;
}

class employee extends person {

public function __construct($name, $age, $height, $weight, $salary, $startdate)
{
parent::__construct($name, $age, $height, $weight);

$this->salary = $salary;
$this->startdate = $startdate;

}

public function get_salary()
{
return $this->salary;
}

public function get_startdate()
{
return $this->startdate;
}

}

$pete = new employee(‘Pete Coles’, 22, ‘6’, 200, 20000, 2012-06-04);

echo(“<br/>”);

print_r($pete);

?>

magic_quotes, addslashes(), and stripslashes() and PHP 6

magic_quotes_gpc, when on, automatically adds slashes to all GET/POST/COOKIE data so that you don’t need to use addslashes() before using GET/POST/COOKIE data in MySQL queries, etc. (e.g. with magic_quotes_gpc OR addslashes(), I’m becomes I\\’m). Well, magic_quotes_gpc is no convenience and just complicates things!

Since magic_quotes_gpc can be on or off, you don’t know whether to use addslashes() or not. You don’t want to use addslashes() when magic_quotes_gpc is on because you’ll add too many slashes (e.g. I’m becomes I\\\\’m), which is bad. Use addslashes() if magic_quotes_gpc is off, and don’t if it’s on (you can find out its setting with get_magic_quotes_gpc()). But you can’t use the same code all the time. One workaround is something such as:

if (!get_magic_quotes_gpc()) { $txt = addslashes($txt);

Using magic quotes has always been confusing and in PHP 6 they are apparantly going to remove it.  So it is better to program your scripts without using get_magic_quotes_gpc at all.

Simple class definition in PHP

Here is a simple class definition in PHP

<?php

class Box {

private $_length;
private $_width;
private $_height;

public function Box($length, $width, $height) {
$this->_length = $length;
$this->_width = $width;
$this->_height = $height;
}

public function volume() {
return $this->_length * $this->width * $this->_height;
}

}

//Example of using the class
$myBox = new Box(20,10, 5);
$myVolume = $myBox->volume();
echo($myVolume);

?>

The definitions of each access modifier :

public. Public members can be accessed both from outside an object by using $obj->publicMember and by accessing it from inside the myMethod method via the special $this variable (for example, $this->publicMember). If another class inherits a public member, the same rules apply, and it can be accessed both from outside the derived class’s objects and from within its methods.

protected. Protected members can be accessed only from within an object’s method—for example, $this->protectedMember. If another class inherits a protected member, the same rules apply, and it can be accessed from within the derived object’s methods via the special $this variable.

private. Private members are similar to protected members because they can be accessed only from within an object’s method. However, they are also inaccessible from a derived object’s methods. Because private properties aren’t visible from inheriting classes, two related classes may declare the same private properties. Each class will see its own private copy, which are unrelated.

Regular Expressions Part 4 – Array Processing

PHP’s preg_split() function enables you to break a string apart basing on something more complicated than a literal sequence of characters. When it’s necessary to split a string with a dynamic expression rather than a fixed one, this function comes to the rescue. The basic idea is the same as preg_match_all() except that, instead of returning matched pieces of the subject string, it returns an array of pieces that didn’t match the specified pattern. The following example uses a regular expression to split the string by any number of commas or space characters:

<?php
‘——————————————————
$keywords = preg_split(“/[\s,]+/”, “php, regular expressions”);
print_r( $keywords );
‘——————————————————
?>

Another useful PHP function is the preg_grep() function which returns those elements of an array that match a given pattern. This function traverses the input array, testing all elements against the supplied pattern. If a match is found, the matching element is returned as part of the array containing all matches. The following example searches through an array and all the names starting with letters A-J:

<?php
‘——————————————————
$names = array(‘Andrew’,’John’,’Peter’,’Nastin’,’Bill’);
$output = preg_grep(‘/^[a-m]/i’, $names);
print_r( $output );
‘——————————————————
?>

Regular Expressions Part 3 – Replacing Patterns

Replacing Patterns

In the examples in Part 2, we have searched for patterns in a string, leaving the search string untouched. The preg_replace() function looks for substrings that match a pattern and then replaces them with new text. preg_replace() takes three basic parameters and an additional one. These parameters are, in order, a regular expression, the text with which to replace a found pattern, the string to modify, and the last optional argument which specifies how many matches will be replaced.
preg_replace( pattern, replacement, subject [, limit ])

The function returns the changed string if a match was found or an unchanged copy of the original string otherwise. In the following example we search for the copyright phrase and replace the year with the current.
<?php
echo preg_replace(“/([Cc]opyright) 200(3|4|5|6)/”, “$1 2007”, “Copyright 2005”);
?>

In the above example we use back references in the replacement string. Back references make it possible for you to use part of a matched pattern in the replacement string. To use this feature, you should use parentheses to wrap any elements of your regular expression that you might want to use. You can refer to the text matched by subpattern with a dollar sign ($) and the number of the subpattern. For instance, if you are using subpatterns, $0 is set to the whole match, then $1, $2, and so on are set to the individual matches for each subpattern.

In the following example we will change the date format from “yyyy-mm-dd” to “mm/dd/yyy”:
<?php
echo preg_replace(“/(\d+)-(\d+)-(\d+)/”, “$2/$3/$1”, “2007-01-25”);
?>

We also can pass an array of strings as subject to make the substitution on all of them. To perform multiple substitutions on the same string or array of strings with one call to preg_replace(), we should pass arrays of patterns and replacements. Have a look at the example:
<?php
$search = array ( “/(\w{6}\s\(w{2})\s(\w+)/e”,
“/(\d{4})-(\d{2})-(\d{2})\s(\d{2}:\d{2}:\d{2})/”);

$replace = array (‘”$1 “.strtoupper(“$2”)’,
“$3/$2/$1 $4”);

$string = “Posted by John | 2007-02-15 02:43:41”;

echo preg_replace($search, $replace, $string);?>

In the above example we use the other interesting functionality – you can say to PHP that the match text should be executed as PHP code once the replacement has taken place. Since we have appended an “e” to the end of the regular expression, PHP will execute the replacement it makes. That is, it will take strtoupper(name) and replace it with the result of the strtoupper() function, which is NAME.