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
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-06-2007, 11:26 PM   #1 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default [Tip] Variable Prefixes

Something i learnt, fairly recently but has already been a huge timesaver, and keeps scripts to a strict standardised programming method is variable prefixes.

Variable prefixes are 1 to 2 characters prior to the variable name, in the variable, for example:
$szString instead of $string.

The following are prefixes i use:

m_ Member, this is used as a pre-prefix to show a member variable and is used like: $this->m_iInteger;
g_ Global, this is used as a pre-prefix to show a global variable and is used like: global $g_aGlobal;
a Array ($aArray)
sz String ($szString, $sz looks better than $s before you ask)
i Integer ($iInteger)
f Float ($fFloat)
b Boolean ($bBoolean)
p Object/Pointer ($pPointer)
m Mixed (i.e. could be any value - $mMixed)

Think of the following peices of code:
PHP Code:
<?php
$numbers 
= array(151015);
$count count($numbers);
$addition $numbers[1]+$numbers[2];
$division $numbers[1] / $numbers[2];
$integer $addition+$division;
?>
With the previous snippet of code, you may of noticed that i have thought the $integer would be an integer, but looking a little closer you would notice that division could cause non-integer numbers.

Now if you take the following peice of code:

PHP Code:
$aNumbers = array(151015);
$iCount count($aNumbers);
$iAddition $aNumbers[1]+$aNumbers[2];
$fDivision $aNumbers[1] / $aNumbers[2];
$iInteger $iAddition+$fDivision
Now it is made very clear that the $iInteger would not be an integer, due to the $fDivision clearly not being an integer, thus causing a float result which could cause problems within your application.


This is obviously a very simple example of the benefits of variable prefixes, but imagine using these within multiple thousand line applications, with variables being used across multiple includes, variable prefixes come in much more handy then! Other benefits include, the ability to type-cast/type-enforce and definately readability!
bluesaga is offline  
Reply With Quote
Old 09-06-2007, 11:30 PM   #2 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Thanks Bluesaga.

I always wondered what sz stands for.
Haris is offline  
Reply With Quote
Old 09-06-2007, 11:41 PM   #3 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

No problem, it is a little hard to get used to, from your normal programming, but since i have got use to it, its a lifesaver! Honestly
bluesaga is offline  
Reply With Quote
Old 09-06-2007, 11:44 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Just to elaborate on Bluesaga's point about the automatic typecasting. I wrote a function earlier to automatically extract the prefix from GET, POST, COOKIE and SESSION arrays. You can then enforce the types how you wish - automatically.

It's common knowledge that most sites are hacked because a user is allowed to set the values of GET and POST (And less so COOKIE) to whatever they desire and thus causing all sorts of problems such as SQL injection attacks.

PHP Code:
function getPrefix($szPointer)
{
    
$aVariable str_split($szPointer);
    
$szPrefix '';
    
    foreach(
$aVariable as $szVariable)
    {
        
$szOrd ord($szVariable);
        
        if(
$szOrd >= 65 && $szOrd <= 90)
        {
            break;
        }
        
        
$szPrefix .= $szVariable;
    }
    
    return 
$szPrefix;

This function will return the prefix. I would use it the following scenario:

PHP Code:
function g($szPointer)
{
    
$szValue $_GET[$szPointer];
    
    switch(
getPrefix($szPointer))
    {
        
/* Code Typecasting */
    
}

The switch will contain the array pointer's prefix, such as sz or i. I would recommend then using the CTYPE_* functions.
__________________
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 09-07-2007, 06:26 AM   #5 (permalink)
The Contributor
 
localhost's Avatar
 
Join Date: Apr 2007
Location: Hampshire
Posts: 28
Thanks: 1
localhost is on a distinguished road
Default

Wow thanks both of you, I never knew anything about this so I will be reading again tonight when I get home from school and will hopefully learn alot of information about it, Thanks again!
__________________
Send a message via MSN to localhost Send a message via Skype™ to localhost
localhost is offline  
Reply With Quote
Old 09-07-2007, 10:36 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
PHP Code:
function getPrefix($szPointer)
{
    
$aVariable str_split($szPointer);
    
$szPrefix '';
    
    foreach(
$aVariable as $szVariable)
    {
        
$szOrd ord($szVariable);
        
        if(
$szOrd >= 65 && $szOrd <= 90)
        {
            break;
        }
        
        
$szPrefix .= $szVariable;
    }
    
    return 
$szPrefix;

I'm just wondering why you went the route of split/foreach when a simple RegEx would be more readable (personally, I find it easier to glance at a pattern than work through a sequence of loops) and probably quicker? Also, in the case of 'pointers' without an uppercase character (e.g. random) the full string would be returned -- I'd have thought that in that sort of case, there'd logically be no prefix.
Salathe is offline  
Reply With Quote
Old 09-07-2007, 10:54 AM   #7 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

I agree that the RegEx could probably be more readable (if you know a bit of RegEx, that is), however, I was always under the impression that you can't beat regular loops in terms of speed. Afterall, by looping yourself you know exactly how many loops are required. In all honesty though, I have no idea how many loops a RegEx makes. This question probably deserves a new thread to itself, what is quicker, custom loops or RegEx :)
Karl is offline  
Reply With Quote
Old 09-07-2007, 12:18 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Karl View Post
I agree that the RegEx could probably be more readable (if you know a bit of RegEx, that is), however, I was always under the impression that you can't beat regular loops in terms of speed.
PHP Code:
// My version of Wildhoney's getPrefix function
function getPrefixSalathe($szPointer)
{
    if (
preg_match('/^([a-z_]+)[A-Z]?/'$szPointer$aVariable))
        return 
$aVariable[1];
        
    return 
'';

After a quick test here are my results based on calling the function with the single argument "szString".

Calling with "szString" over 100,000 runs:
Wildhoney - 1.3253 seconds
Salathe - 0.9695 seconds

Then again, what real difference is 0.00000356 seconds per function call going to make? :p

As far as being, "under the impression that you can't beat regular loops in terms of speed," that is a good position to hold as a general rule. Sometimes looping through every character (or every item in an array, or whatever) is the clearest, simplest and fastest way to get things done. It is quite easy to get the RegEx parser bogged down - with great power comes great responsibility (to keep things moving slickly).

Finally, it would be slightly more efficient (~0.1141s faster over 100,000 runs) for Wildhoney's function to forgo the str_split and loop through the characters in the string directly if you didn't want to go the RegEx route.
Salathe is offline  
Reply With Quote
Old 09-07-2007, 12:23 PM   #9 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Thanks for that information Salathe, it's very interesting to know! I must admit, I honestly thought RegEx would have been slower :o
Karl is offline  
Reply With Quote
Old 09-07-2007, 12:25 PM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by Karl View Post
Thanks for that information Salathe, it's very interesting to know! I must admit, I honestly thought RegEx would have been slower :o
Me too, actually. Will definitely go down the RegEx path now though.
__________________
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 09-07-2007, 03:28 PM   #11 (permalink)
The Contributor
 
localhost's Avatar
 
Join Date: Apr 2007
Location: Hampshire
Posts: 28
Thanks: 1
localhost is on a distinguished road
Default

Wow, That's not a very big time difference... All the books I have ever read say that Regex is slower... Maybe not :p
__________________
Send a message via MSN to localhost Send a message via Skype™ to localhost
localhost is offline  
Reply With Quote
Old 09-07-2007, 10:01 PM   #12 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

It depends on what usage, if you have look aheads and look behinds regex will definately be slower, it just depends how you want to use it.
bluesaga is offline  
Reply With Quote
Old 10-24-2007, 01:28 AM   #13 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Here's a question for you people which I've been pondering and musing over. What prefix would you use for an unknown variable type?

PHP Code:
$vMyVar 'Unknown'// Varies
$uMyVar 'Unknown'// Unknown 
What else is there? It really needs one that looks good - v and u don't, to me.
__________________
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 10-24-2007, 09:36 AM   #14 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Doesn't that really get classified by:

PHP Code:
$mMixed "Mixed Variable, could be array, int, float etc...."
bluesaga is offline  
Reply With Quote
Old 10-24-2007, 10:24 AM   #15 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

I agree with Bluesaga, I always use the m prefix for mixed vars.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 10-24-2007, 11:58 AM   #16 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The PHP manual uses the term "mixed" to denote uncertain type. So yes, an m prefix would seem the most appropriate choice (and is the one I use).
Salathe is offline  
Reply With Quote
Old 10-24-2007, 12:05 PM   #17 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

In that case, m it is!
__________________
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-05-2007, 04:14 AM   #18 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Thanks for the tip. I've been using it lately, and it makes my code seem much more "professional". :)
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 11-05-2007, 12:57 PM   #19 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Yea :) it does make code a lot more professional looking - and also a lot easier to read! It shows that you know what the functions return if you set data types, and so when you skim over it you can instantly deduce what's holding what.
__________________
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-12-2007, 01:35 AM   #20 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

What prefix would you guys use for MySQL resources such as a query result?
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew 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 03:39 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design