Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

closed as primarily opinion-based by Mathew Foscarini, MichaelT, Corbin March, GlenH7, Bart van Ingen Schenau Aug 17 '13 at 11:16

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Pick one and stick with it. It doesn't matter which one you'll pick, as long as you stick with it. Same with tabs vs spaces. –  Yannis Rizos Apr 28 '13 at 9:54
2  
I strongly agree with the opinion prevalent in the linked discussions: one should follow the convention. That's why I've limited the scope of the question to PHP. However, I'm starting to wonder whether there is a dominant naming convention in PHP. –  exizt Apr 28 '13 at 9:55
4  
There's not, naming conventions is yet another of those little inconsistencies that make PHP so wonderful to work with. PSR is the one effort that I think is worth following, but... it's completely up to you. Related questions: Are there standard style guides for PHP? & Why Bootstrap 3 changes camelCase to dashes - is it more readable?. –  Yannis Rizos Apr 28 '13 at 9:58
    
This question might help you: programmers.stackexchange.com/questions/149303/… –  Jason Holland Apr 29 '13 at 16:21

4 Answers 4

up vote 10 down vote accepted

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.

share|improve this answer
    
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. –  Rudolph Gottesheim Aug 14 '13 at 14:50
    
@RudolphGottesheim Good point. I have edited the answer accordingly. –  Residuum Aug 16 '13 at 22:04

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.

share|improve this answer

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, bare 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 bare that in mind.

share|improve this answer

This is depends on the programmers/developers as well as on which framework or open source he or she is working on. For eg, in Yii framework it is advisable to use camel case, where as in the Codeigniter framework people mostly follow variable names with underscores. But one thing is for sure that what ever framework you use the variable name must be user understandable and must be identified with it's usage.

share|improve this answer

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