50

If I wanted to write a really long variable name like:

$this_is_my_variable_that_does_something_in_the_following_function_and_provides_some_information_with_which_the_function_relies_upon_to_do_all_the_work_it_needs = null;

would that work? same question for function/method names

james
  • 3,543
  • 8
  • 31
  • 39
  • 14
    You should make that camel case, otherwise it is hard to read. – Parris Varney Dec 30 '10 at 20:04
  • 30
    @PMV: `$thisIsMyVariableThatDoesSomethingInTheFollowingFunctionAndProvidesSomeInformationWith WhichTheFunctionReliesUponToDoAllTheWorkItNeeds = null;` Yeah, that looks easy to read. I can say it in a single breath! **\*inhales\*** – BoltClock Dec 30 '10 at 20:05
  • 2
    @BoltClock: Probably capitalize NULL too, since it's a constant. Then it would be a perfectly readable and sensible statement. Too bad punctuation isn't allowed though. – Parris Varney Dec 30 '10 at 20:08
  • @PMV: Yeah I forgot that. I always capitalize it in my code. – BoltClock Dec 30 '10 at 20:13
  • 1
    A good question, but trivial to answer for yourself. Did you try it? – HappyDog Sep 16 '19 at 11:10
  • As PHP doesn't define any limit. I do prefer ANSI standards, it recognizes a length of 31 characters for a variable name. However, the length should not be normally more than any combination of eight alphabets, digits, and underscores. – Amitesh Bharti Dec 16 '21 at 07:11

6 Answers6

246

Generally, such a limit is imposed by the threat of violence from other folks who interact with your code.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • 9
    I just had a good laugh reading your explanation. The exact same thing often happens if your code only uses only single-letter variables like `$i`, `$n` or `$k`. – Jocelyn Dec 01 '12 at 02:44
  • So, `if (strlen($varname) < 4 || strlen($varname) > 16) { SKINNERRRRRR(!!!!!1); }` – thomasrutter Jan 14 '14 at 14:16
  • 2
    @RobinJ By choice of the posting user, like every other accepted answer on the site. It's also, IMO, a perfectly accurate answer - technological limitations aren't the only ones. Also, it's five years old... why ask now? – ceejayoz Apr 10 '15 at 16:56
99

From the documentation:

Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores

The same is the case for function names, as stated here.

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • So writing 200 underscores on a line (and a semicolon at the end) is perfectly valid as long as notices are switched off! Time to introduce my new separators :) – dkellner Nov 23 '18 at 15:10
  • I would prefer the ANSI recommendation for the safer side given for C after all PHP is written in c (a length of mo more than 31 characters for a variable name.) – Amitesh Bharti Dec 16 '21 at 07:13
18

The limit in variable/function/method/class name length does not exist.

Comments above states that that this property should not be exploited. That is true only when it comes to human readable/human maintainable code.

However, this is extremely useful feature of PHP, that is exploited very well in a lot of very popular projects, such as Twig per example, which generates classes, example (a snippet):

class __TwigTemplate_9601167421099bbb502d119e674fed3fb71769eef256903648c9c2b4b6faa436 extends \Twig_Template {

    protected function doDisplay(array $context, array $blocks = array())
    {
        $__internal_0abebc74dd811fd7b4cfa4c6a2fdf870d7723c04e8daf6926b04914d6644935f = $this->env->getExtension("native_profiler");    
    }

}

I had opportunity to benefit from same property as well in my projects.

In conclusion:

  • There is no limit in var/func/class name length
  • It is extremely useful language feature
  • There is a context for its usage, of course, it is not for every day work
Nikola Svitlica
  • 622
  • 6
  • 14
3

There is no limit - but it is highly not suggested as it creates unreadable code...

zsalzbank
  • 9,685
  • 1
  • 26
  • 39
  • 2
    All the opposite, it does create very readable code indeed. It's much more expressive `$arrivalDateTimeWithUnknownTimeZoneAndIncorrectlyAssignedUtcTimeZone` (real example from our production code) than just `$arrivalDateTime`. Reason: In the second one the human reading your code will have to ask you if there's a bug or not because it has an Utc time zone while in reality the time zone was actually unknown. In the first case it's clear it was on purpose. – Xavi Montero Jul 07 '18 at 23:28
  • 4
    It makes very unreadable code and makes the code harder to type. There's a reason why comments exist, don't put all the information in the variable name. – brianush1 Jul 12 '18 at 17:33
  • 1
    On the other hand, comments are harder to maintain. When you have the reason for a variable on it's own name, it makes life easier. It actually takes less time to read a long variable, than always having to go to the definition and reading a comment. – Mateus Viccari Jul 26 '19 at 17:33
  • Big variable/function/etc names run the risk of misspelling; forget a double-r or a capital-Z on `$arrivalDateTimeWithUnknownTimeZoneAndIncorrectlyAssignedUtcTimeZone` and try to find the error on a heavy coding day. If you need to travel such a variable across hundreds of lines of code I would suggest describing it by code, like: `$arrivalDateTime = array("timestamp" => 3213546, "timeZone" => false, "assignedUTC" => false);` – Geo Halkiadakis Nov 22 '19 at 18:11
3

PHP does not pose a length limit on it's identifiers.

That said, I'm not sure why anybody would ever want to create a 160 character variable name. I hope this is a hypothetical question.

Craige
  • 2,882
  • 2
  • 20
  • 28
  • 3
    Well I was thinking of writing of framework/coding standard that didn't use comments and instead relied on using really descriptive variable and function names =P – james Dec 30 '10 at 20:03
  • 12
    You should name it Framework/CodingStandardThatDoesn'tUseCommentsandInsteadReliesonUsingReallyDescriptiveVariableandFunctionNamesCMS. – ceejayoz Dec 30 '10 at 20:08
  • Ugh. So you expect a long variable name to substitute for a comment that explains why someone did something bizarre? Good luck with that. – Brian Clapper Dec 30 '10 at 20:09
  • Maybe it's just dynamic stuff identifiers ;) – Thiago Macedo May 26 '14 at 15:51
  • There is a usage context - auto generated vars/classes/functions – Nikola Svitlica Sep 14 '15 at 09:26
  • Not 160, but this is real production code for me: `$arrivalDateTimeWithUnknownTimeZoneAndIncorrectlyAssignedUtcTimeZone`. +1 for @ceejayoz and clean code rulez! – Xavi Montero Jul 07 '18 at 22:29
-3

PHP doesn't have any restriction for variable name. And my suggestion is that variable name must consist the definition of the data which you are gonna store into it.

  • 3
    You answered a question almost 9 years later, with adding no new points. Please consider answering an old question only if you can provide a different solution/answer. – MAChitgarha Jul 24 '20 at 13:46