Suggest good variable names that represent their values ​​(php)

I think that programmers should have some interesting convention on variable names. I have seen many places that say that some variable names are very efficient, like

common variable names

  • $ link
  • $ db
  • $ connection
  • $ request
  • $ STMT
  • $ SQL
  • $ QRY
  • $ exit
  • $ result
  • $ list

Therefore, please offer me some good names for the variable, because all the time I have to write $x, $y , etc., if I want to save something instantly on the page ... which are not even relevant, therefore, please suggest me good variable names

one more question:

The values ​​of long file names and variables / classes / objects before and after = affect the performance of the php page. for example, to get some data from db, which 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, if I change the name of the class and delete the tabs before and after = and change the methods a bit

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

Does this affect page performance? This is my real question.

+4
source share
7 answers

I do not know if the list will be useful.

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

Make sure it is consistent, i.e. camelCase , PascalCase , under_score or gluedtogether .

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

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

As for your update , spaces should not affect performance. All this should be marked at the stage of analysis and ignored outside the lines.

Definitely micro-optimization. If you are also so concerned about speed, you should match quotes with strings without any interpolation variable. This, of course, is also micro-optimization.

I would never change this, unless it was a rare chance, this was the bottleneck of your application, and this had to be solved.

+4
source

Long names will have a tiny tiny effect on parsing, not even enough to justify using a name like $ v. A name that is long enough to be descriptive, but short enough to dial a bunch of times, will save manifest cycles that cost a lot more these days than processor cycles.

+1
source

You really should take a look at:

PHP coding standards

+1
source

The best variable name is a name that reflects the target variable.

A good example can be found on the foreach page. When repeating the array, we set 2 variables, $key and $value . These are good names because they describe a variable value. You cannot confuse a key with a value.

makes a long file / variable / class / object names and tabs before and after = affects the performance of the php page.

Not.

0
source

The answer to your β€œreal question” about whether renaming variables will affect page functionality is β€œNo” (unless there is something funny about PHP that I don’t understand). Variable names mean nothing to the compiler / interpreter / everything that does what you wrote. Creating variable names that are understandable enough for someone who needs to look and understand should be the goal you have at all times when writing code; this does not affect runtime, but facilitates future maintenance.

Providing everything containing a list of names of "good" variables would be impossible. Alex offers some good general pointers to variable names, and a quick Google search can direct you to many places praising the virtues of providing variable names that make sense when you look at them after a week. Use names that are short but describe the purpose of the variable.

0
source

What more variable names affect performance in PHP?

Well, your PHP file will be several 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. In addition, performance is not affected. It would be foolish to shorten your variable names to improve performance.

0
source

I would suggest always using the same short variable names for things that you do most of the time, for example, if you write tons:

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

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

The variables on your list are good for these general tasks.

Zend has very good coding standards , mainly for classes.

0
source

Source: https://habr.com/ru/post/1311275/


All Articles