TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-19-2007, 02:51 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Salathe : 11-29-2007 at 10:47 PM. Reason: Minor code fixes.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-29-2007, 06:09 PM   #2 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Any ideas if type casting adds a lot of overhead?
bdm is offline  
Reply With Quote
Old 11-29-2007, 06:42 PM   #3 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
sketchMedia is on a distinguished road
Default

Good question, i will have to do some research.
__________________
sketchMedia is offline  
Reply With Quote
Old 11-29-2007, 09:20 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-29-2007, 11:32 PM   #5 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
sketchMedia is on a distinguished road
Default

That was my first thought because your not calling an extra function, and then returning a value.
__________________
sketchMedia is offline  
Reply With Quote
Old 11-30-2007, 02:29 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-30-2007, 02:44 AM   #7 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
sketchMedia is on a distinguished road
Default

yep, tis typical, making PHP less and less standardised which in turn makes developers more and more stressed.
__________________
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:22 PM.

 
     

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