66

The consensus seems to be that one should follow the convention of the platform they're developing for. See:

Underscore or camelcase?

Naming conventions: camelCase versus underscore_case?

However, PHP doesn't seem to strictly follow any convention internally (no surprises there), even for methods and functions (e.g. mysqli::set_local_infile_default, PDOStatement::debugDumpParams); however, underscores seem to be dominant in function names.

However, what I couldn't find was this: what's the dominant naming convention for variables in PHP?

8

4 Answers 4

66

There is no definitive naming convention in PHP, and they differ by framework:

So: Use whatever your framework uses or create your own naming convention.

At least for function names and class methods, there is a one thing to consider, but some frameworks discard it: PHP is case insensitive in that case, so aTonalFunction() and atonalFunction() are both calls to the same function.

4
  • 2
    Nothing against your methond naming argument, but that is absolutely not the convention. All modern frameworks and libraries like Zend Framework 2, Doctrine 2 and Symfony 2 use camelCase for method names. Aug 14, 2013 at 14:50
  • @RudolphGottesheim Good point. I have edited the answer accordingly.
    – Residuum
    Aug 16, 2013 at 22:04
  • 1
    Interesting. I came here because I found the coding convention of wordpress to be ungainly. Now I see at least they meant it to be ungainly
    – Kirby
    May 14, 2015 at 4:06
  • 4
    Internally, PHP itself uses Underscores for variables and functions, camelCase for Methods, and PascalCase for Classes, as stated in this PHP documentation page (userlandnaming.rules), and as spelled out in this PHP coding standards document. That said, various frameworks follow different conventions of their own. Apr 6, 2020 at 18:11
19

As coding for more than ten years with PHP, I can see a change from underscore to camelCase. Especially the bigger frameworks like Zend and Symfony build up on camelCase also when they don't prohibit underscore, but all the core function is camelCase.

You can see a lot of underscore solutions in older systems, like the old Typo3 branch (Typo3 Neo changed to camelCase, too).

But there is definitely no strict rule, which is used by everyone, that's just what I can see over the past few years.

1

This depends on the programmers/developers as well as the framework or open source they are working on. For example, in the Yii framework, it is advisable to use camel case whereas in the Codeigniter framework, most people follow variable names with underscores. One thing for certain is that regardless of which framework you use, the variable name must be understandable and identifiable.

0

Personally, this can go either way. Some programmers prefer the CamelCase way of writing, others prefer underscore... So asking a question like this posses it's irrelevance as each programmer will have their own different opinion... Personally, I have the habit of doing both depending on the situation...

Situation one:

You have joined a programming team and have joined an on-going project, you notice that an example variable is set out:

$theString = "This is a camelcase example";

But you have a preferred style of:

$the_string = "This is not a camelcase example"; 

in a situation like this, it would be best to keep to camelcase.

Situation two:

You wish to start a project solo.

In this sort of case... It just goes down to what you feel comfortable with, bear in mind bad practices in terms of:

$theString = "This is a sample String"; 
$the_string = "This is another sample string within the same project"; 

This can easily cause unexpected output.. Just bear that in mind.

1
  • 9
    FYI, what you are referring to is PascalCase - camelCase starts with a lower case letter.
    – John Weisz
    Dec 16, 2015 at 0:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.