Home » Php » PHP – Function/variable naming

PHP – Function/variable naming

Posted by: admin July 12, 2020 Leave a comment


I have read a lot of popular standards manuals for open source PHP projects.

A lot enforce underscores for variables spaces, and a lot enforce camelCase.

Should global functions and variables be named differently to class methods/properties?

I know the most important thing is consistency, but I’d like to hear some thoughts on this.

What would you recommend?

How to&Answers:

I find camelCase a little more pleasant to type, because I find the underscore a bit awkward to type.

Don’t use global variables.

I avoid procedural coding in PHP, I find OOP is easier to keep things organized. Besides, doesn’t PHP have enough stuff in it’s global namespace already?

Generally I try to stick to:

  • Classes are StudlyCaps singular or plural nouns, as appropriate: Item, Row, DB, Items.
  • Variables are lowercase nouns, singular or plural depending on what they hold: $column, $name
  • Constants are singular upper-case nouns: DEBUG, TYPE_FOO.
  • Methods are camelCase, and begin with singular verbs (get, perform, do), followed by a noun (singular or plural) describing what it operates on or returns (getThing(), getThings())

It definitely depends on what you’re coding for. If I’m coding PHP or PEAR, I use camelCase. If I’m doing Python/Django, I use under_scores. If I’m writing ELisp, I use dashed-separators.


In PHP itself, almost every native function is underscore separated. Most of the PHP code examples in the documentation are underscore separated.

In most languages I think Camel or Pascal Casing is more appropriate, but I think there’s clear history for using underscore separation in PHP.


Zend Frameworks naming convention (Which is based on PEAR) is probably the closest you come to a standard in the PHP world. Personally, I prefer to use lowercase_underscore for variable names, but otherwise I mostly follow ZF’s convention.

Update on 10 year anniversary:

These days, there is a standard, which is largely accepted within the community. You should stick with that:



Yes, the most important thing is consistency. If you are the lone developer, stick with a method. If you are working with a team, talk to the other team members. Differentiating between globals, functions/methods and classes will make reading the code much easier. For some people camelCase is easier than using_underlines so your team needs to discuss the options and pick a style.


Note: I use underscores for my MySQL table_names, I use UpperCamelCase for MySQL field names:

Normally I use $lowerCamelCase for variable names and class properties, but if it contains the value from a field, I use the $UpperCamelCase field name, or if it is an array of data from a table, I’ll use the $table_name. This way I can easily grep for SomeField or some_table and find everything referring to it.

You don’t have to use this exact system, but being able to search for all references to a field or table is a huge benefit.


I used to prefer to use camelCase, but for the sake of consistency in bigger applications, I have adopted CodeIgniter’s style guide.

Even if you don’t use their framework, you can appreciate the work that went into defining clear and comprehensive styles: http://codeigniter.com/user_guide/general/styleguide.html


My goal – whatever the specific format of the name – is adding more information. Does the name improve the understanding of the code and/or express something important?

If it does, great, then you’ve succeeded in it.

If the name doesn’t add anything, why did you bother naming it?

I wrote on this one earlier this week:



I would recommend reading the PEAR Coding Standards. Since PEAR is the official PHP Extension and Application Repository, it can be considered the language’s official coding standard.