![]() |
[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:
Now if you take the following peice of code: PHP Code:
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! |
Thanks Bluesaga.
I always wondered what sz stands for. |
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
|
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:
PHP Code:
|
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!
|
Quote:
|
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 :)
|
Quote:
PHP Code:
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. |
Thanks for that information Salathe, it's very interesting to know! I must admit, I honestly thought RegEx would have been slower :o
|
Quote:
|
Wow, That's not a very big time difference... All the books I have ever read say that Regex is slower... Maybe not :p
|
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.
|
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:
|
Doesn't that really get classified by:
PHP Code:
|
I agree with Bluesaga, I always use the m prefix for mixed vars.
|
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). |
In that case,
m it is! |
Thanks for the tip. I've been using it lately, and it makes my code seem much more "professional". :)
|
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.
|
What prefix would you guys use for MySQL resources such as a query result?
|
| All times are GMT. The time now is 09:50 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0