We're going to be dealing with type juggling in this article. This is the process of instructing PHP how to handle the value after the type juggling. Type juggling can do absolutely all sorts, from converting integers to floats, arrays to objects, and even strings to binary as of PHP 5.2.1. It can also save you calling various functions and thus making your script quicker!
This is the type juggling syntax in PHP:
php Code:
$szMyString = (string) 5;
The above type juggling line will assign
$szMyString the value of "5". That is, the integer, 5, in quotes, making it a string. Whereas 5 that is not encapsulated in quotes would be an integer. That, my friends, is about all there is to the syntax of type juggling, but in what situations would we use it?
There are various situations where type juggling becomes useful. Let's imagine we have a basic function that checks the syntax of a URL. As this is a simple function it merely uses a little regular expression inside the
preg_match function to check for the presence of
http://.
php Code:
function validateLink
($szURL){ return preg_match('/^http:\/\/.*$/i',
$szURL);
}
However,
preg_match returns 1 and 0 bits instead of
true or
false. To stick to standards we want to return it as a boolean and thus type juggling makes its valiant entry into our function:
php Code:
function validateLink
($szURL){ return (boolean
) preg_match('/^http:\/\/.*$/i',
$szURL);
}
By simply adding the
(boolean) we are saying that if the function returns a value that is considered false, then
return false, else
return true. We can then use the return value alongside the 3 equal signs which is the identical operator:
php Code:
if(validateLink('http://www.google.com/') === true)
{
}
We can be so sure of it returns value as either true or false because we have told it so. This overcomes the problem of the inconsistency of the returns in PHP - something I'm pessimistically assuming they won't be correcting in version 6.
Let's take a look at a few test cases. They do say that getting your hands dirty is the best way to educate yourself!
php Code:
/* Returns: "5" */$mVar1 =
(string
) 5;
/* Returns: true */$mVar2 =
(boolean
) 'Test';
/* Returns: 10 */$mVar3 =
(integer
) 10.
55;
/* Returns: true, because subsequent types are ignored */$mVar4 =
(boolean
) (float
) 15;
/* Returns: object with property Item of value false */$mVar5 =
(object
) array('Item' =>
(boolean
) 0);
/* Returns: array */$mVar6 =
(array) $mVar5;
The first few may appear to be straightforward, and indeed they are, the bottom 2 may not be so straightforward, so I'll elaborate on the workings of those 2.
By converting an associative array to an object instead, we access it differently. If you've read the
article on JSON encoding and decoding then you'll already be familiar with this approach to programming.
From the above example we can access
$mVar5 like so:
Whereas we would access the array stored in
$mVar6 like this:
With OOP becoming rather popular these days, objects have begun to takeover from arrays, however, I doubt arrays will ever be obsolete because they still have a lot of power in their array functions, though objects are certainly encroaching on their dominance.
That's all there really is to type juggling. Although you can never explicitly set the type of a PHP variable, much to my disappoint and dismay, you can instruct PHP on how to deal with a particular value and what to convert it to, if anything.