PHP Tricks for Beginners

The Elusive ===

PHP is what they call a “loosely” typed language. This means that if you have "105", it could mean you have the string “105” or you can use this as a number, adding and subtracting to your heart’s content. One thing that gets a little tricky in PHP because of this fact is the number 0. Since PHP tries its best to determine what you want from your variables, 0 can mean 0 or false…and sometimes this makes things a bit difficult. Luckily we have the ===.

Like other languages == is the comparative operator, but when comparing 0, often times PHP interprets it as false rather than an actual 0. This is where === comes into play. This operator compares not only a value but a type as well. So when you want to compare 0 and make sure it is comparing the integer 0, then it’s time to use the === operator.

$foo = false;

if($foo == 0) {}

if($foo === 0) {}

Is it isset?

One thing that is used a lot in PHP, but I always miss in other languages is the isset function. It is a very powerful and easy way to determine if something exists. What isset is useful for is to determine if a particular variable or element in an array has a value. This may seem a little odd and silly, but it comes in handle a lot. It takes a bit of getting use to, but this function comes in handy a lot.

function PFP($foo)
  //Add a default value for bar,
  //if it is not set.
    $foo[‘bar’] = "foo";


The single greatest function in PHP, and the one I miss the most in other languages, is the empty function. It is such a simple idea, it checks a variable to see it is empty, and returns true or false. It checks for several things, but it is most useful when you want to check a string or array, because no one likes an empty string or array. However, this function also checks for null, false, or 0…which are all considered empty. When developing in PHP, you never go without using the empty function.

function PassValue($foo)
    return false;

Calling Your Functions, The Fun Way

In PHP, it is extremely easy do complex things, like calling functions or class methods with a string… that’s right, with a string. At first this sounds like something a crazy scientist would want to do, but this actually comes in handy quite a bit. First of all, this makes building dynamic libraries very easy. For instance, you may want to pass in a string that references a function you want to call.

To achieve this, we use the function call_user_func, which takes in one argument and a set of parameters. That is, you give it the function to call followed by the parameters you want to pass to that function. Its usage is really best described by example:

function CallMe($param) {}

call_user_func(‘CallMe’, $myParam);

So there are four useful PHP tricks that you can use. It’s true they are less tricky and more just things you need to know, but trust me when I say they can be used in a lot of different ways. This wraps it up for this tutorial, but just remember when you need coding help, all you have to do is Switch On The Code.

Leave a Reply

Your email address will not be published.