tags:

views:

105

answers:

7

Hi all, it may be wrong place to ask this question, but i hope u all programmer must have the interesting naming convention of variables. i have seen many places that some variable names are very good n effective like

common variable names

  • $link
  • $db
  • $connect
  • $query
  • $stmt
  • $sql
  • $qry
  • $output
  • $result
  • $list

so please suggest me some good names for variable , bcoz all time i have to write $x, $y etc.. if i want to save something instantly on page...that are even not relevant, so please suggest me good variable names

one more question:

does long file/variable/class/object names and tabs before and after = effects performance of php page. for example to get some data from db i used to write below code with long names

include_once    "../libs/databaseConnection.class.php";
$objDatabaseConnection      =        new databaseConnection();
$query                      =       some query;
$arrFetchResult             =       $objDatabaseConnection->fetchByAssoc($query);

instead of this, if i change the name of class and remove tabs before and after = and modify methods (a little bit)

include_once "../libs/db.config.php";
$db=new dbClass();
$qry=some query;
$output=$db->fetch($qry,"assoc");

does this affects the performance of page? this is my real question.

+3  A: 

I don't know if a list would be useful.

Just think of a name that accurately best describes the variable's contents.

Be sure to be consistent, i.e. camelCase, PascalCase, under_score or gluedtogether.

Also, don't negate things like $notLoggedIn, because then it gets confusing with something like this

if ( ! $notLoggedIn) {
    // user is logged in
}

Concerning your update, whitespace should not affect performance. It should all be tokenised at parse stage and ignored outside of strings.

Definitely a micro optimisation. If you are so concerned with speed too, you should single quote your strings with no variable interpolation. This of course is a micro optimisation too.

I would never ever change this unless on a rare chance it was the bottleneck of your application and it needed to be addressed.

alex
yes, but i want to collect some good name so that it looks good when we see our code after many days.
diEcho
`$fluffy` will bring a smile after a few days... :)
alex
My favorite one that i've seen, and i can't remember where is $hit :)
iangraham
i updated my questions. please reponse
diEcho
A: 

The best variable's name is the name, that reflects variable's purpose.

Good example can be found on the foreach manual page. While iterating an array, we set up 2 variables, $key and $value. That's good names as they describes variable's meaning. You cannot confuse a key with a value.

does long file/variable/class/object names and tabs before and after = effects performance of php page.

No.

Col. Shrapnel
A: 

The answer to your "real question" of whether renaming variables will affect the functionality of the page is No (unless there's something ridiculous about PHP that I don't understand). Variable names mean nothing to the compiler/interpreter/whatever that's executing what you wrote. Making variable names that are descriptive enough for someone else to look at and understand should be a goal you have any time you write code; it doesn't affect runtime but it makes future maintenance much easier.

Providing an all encompassing list of "good" variable names would be impossible. Alex offers some good general pointers on variable naming and a quick Google search can direct you to plenty of places extolling the virtues of giving variables names that make sense when you look at them in a week. Use names that are succinct but describe the purpose of the variable.

chaosTechnician
A: 

Do longer variable names affect performance in PHP?

Well, your PHP file will be a few bytes larger if you use longer variable names, so the PHP parser needs to ‘parse’ a few extra bytes. This is just a matter of nanoseconds though. Other than that, performance is not affected. It would be downright silly to shorten your variable names to ‘boost’ performance.

Mathias Bynens
@ Mathias Bynens ,if i have 2000 files and each file have very long variable names( 25-30 on eachpage) so if i shorten all names then many bytes will be deducted( as u tell). isn't it better?
diEcho
@I Like PHP *You're (maybe) doing it wrong* if that's the case. What application has 2000 includes? Are you writing to file instead of using a database? Are you writing to file PHP code?
alex
@ alex i just want to say that more files would have more bytes if u doesn't shorten the varaibles names, thats it. i know no project have 2000 files
diEcho
@I Like PHP Possibly, but if your PHP app runs .02 microseconds quicker, do you think all that re-factoring would be worth it?
alex
+1  A: 

Long names would have a tiny, tiny effect on parsing, not even near enough to justify using a name like $v. A name that's long enough to be descriptive, but short enough to type a bunch of times, will save developer cycles -- which are worth a lot more these days than CPU cycles.

cHao
A: 

I would suggest to use always the same, short variable names for the things you are doing most of the time, for example, if you are writing tons of :

if ($res = mysql_query($sql)) {
    while ($rst = mysql_fetch_array($res)) {...}
}

There is no need to use long and expressive names for those variables !

The variables in your list are good for these common tasks purposes.

Zend has very nice coding standards, mainly for classes.

mexique1
+1  A: 

You should really have a look at:

PHP Coding Standards

Sarfraz
@Sarfaraz Thanks for the valuable link
diEcho