TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   Using Type Juggling/Type Casting to Modify Data Types (http://www.talkphp.com/tips-tricks/1499-using-type-juggling-type-casting-modify-data-types.html)

Wildhoney 11-19-2007 02:51 PM

Using Type Juggling/Type Casting to Modify Data Types
 
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:

php Code:
echo $mVar5->Item;

Whereas we would access the array stored in $mVar6 like this:

php Code:
echo $mVar6['Item'];

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.

bdm 11-29-2007 06:09 PM

Any ideas if type casting adds a lot of overhead?

sketchMedia 11-29-2007 06:42 PM

Good question, i will have to do some research.

Wildhoney 11-29-2007 09:20 PM

It's actually supposed to be quicker than the function equivalents. I'm not saying you should use it for everything, but in certain circumstances it makes the code much better in terms of returning 1/0 as true/false.

sketchMedia 11-29-2007 11:32 PM

That was my first thought because your not calling an extra function, and then returning a value.

Wildhoney 11-30-2007 02:29 AM

I look forward to the binary data type using type juggling although I have the new PHP and I couldn't seem to get it working. They also introduced a strange short-hand method of adding the letter b before the item you wish to convert. Very odd if you ask me, though that's Zend for you - always making PHP less and less standardised.

sketchMedia 11-30-2007 02:44 AM

yep, tis typical, making PHP less and less standardised which in turn makes developers more and more stressed.


All times are GMT. The time now is 10:31 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0