• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

Simple question about PHP variable-naming conventions

Having come from Java, the common naming convention developers use is the camelback notation, e.g. firstName, companyName, etc.

I'm going through a PHP book right now, and I notice that the author uses all lower case, multiple words being delineated by an underscore, e.g. first_name, company_name, etc.

I'm actually transitioning from Java to PHP, and I'm just curious. What is the MOST common variable naming convention PHP developers use, if there is one?

Only people who are working as PHP developers should respond, please.

Thanks.
0
elepil
Asked:
elepil
  • 5
  • 4
  • 3
  • +3
5 Solutions
 
GaryCommented:
http://www.php.net/manual/en/language.variables.basics.php

The common practise is lowercase, and if it makes it easier to read a variable name then use underscores to separate the words - but it is all developer preference.
Variables are case sensitive
0
 
Dave BaldwinFixer of ProblemsCommented:
Gary's right and I'd put up that link if he didn't.  I often use camelCase because it makes the names stand out in a page of otherwise lower case text.  I would not use what I see in a lot of "examples" on the internet because I don't think they are good examples.  I often (for myself again) will add an odd letter as the first character of a variable name to make them unique on the page.  Instead of 'name', I will use something like 'uName' and make all the variables in a form start with the same letter so that they are easy to find and search for.  The word 'name' may be on the page for other reasons but 'uName' is going to be there only as a variable name from my form.
0
 
elepilAuthor Commented:
When I asked my question, I guess I was more of wanting to know what employers MANDATED, and not so much what developers prefer; that's why I stated only people who are professionally working as PHP developers should respond.

As a Java developer, I've worked in places where camelback was stringently mandated, and any deviation (e.g. Hungarian notation or php-style-lower-case) was frowned upon and would get you a scolding.

Can anyone tell me what their employers prefer/require them to use as far as variable-naming?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Cornelia YoderArtistCommented:
I always use all lowercase and no underscores.  The reason is that later in the script, I don't want to always have to refer back to see exactly how I capitalized the variable name.

I also use full words as much as possible.  I will always use  $firstname  rather than  $fn  or  $fname  or  $firstnm.  This pays off strongly in fewer bugs/mistakes, and in much better readability later.

I have worked in a lot of programming languages, and never once has an employer mandated how this should be done, in php or any other language.   Good programming practice is all about readability, maintainability, and self-documentation.
0
 
elepilAuthor Commented:
yodercm, you're lucky you've never encountered a hard-nosed employer as I have.  I will still wait for a few more feedback like yours before I assign points. Thanks for responding.
0
 
GaryCommented:
There is no mandated process, if you have a boss that says he wants it one way I'll show you another that wants it another way.  (And that's when they know whats going on to start with)
If you are looking for a best practise that you should adopt then lowercase letters and nothing else, no underscores, no numbers, no capitals.
0
 
Chris StanyonCommented:
Personally, I use camelCase for variables. A lot of PHP functions are named using all lower case with underscores so I find the code easier to read if variables instantly look different to to function calls. Personal preference, and as has already been said, each development company will have their own preferred standards for coding - not only variable naming, but things like code indentation etc.

Each to their own :)
0
 
GaryCommented:
Personally don't like camelCase but that's probably just because of my eyesight!
0
 
elepilAuthor Commented:
Chris. Yes, I am aware that each company will have their own coding conventions. It is my objective to get a "feel" of what PHP development companies go by. From the responses I'm getting so far, there doesn't seem to be a very stringent standard, which is good.

I personally preferred variants of Hungarian notation where each variable is prefixed by the type (e.g. sName, iAge, fSalary, bNewUser, etc.) as one can tell the data type in one glance, but I've encountered a Java employer who insisted I use camelback.
0
 
elepilAuthor Commented:
Cathal,

camelback notation is okay, but I prefer Hungarian (e.g. sName, iAge, fSalary, bCorrect, etc.)

What surprised me is that some of the respondents wanted lower case with no underscore. If I were to go all lower case, I would be using underscore a lot because 'percent_after_expiration' is, to me, easier to read than 'percentafterexpiration'.
0
 
GaryCommented:
Things like 'percentafterexpiration' I would be using underscores, for vars like firstname I wouldn't be so bothered as it is still quite easy to read.
Haven't seen the 'Hungarian' version in years, I don't think most developers use it anymore, vars can be anything in php, you do not need to declare the vars type, a var can be a number and a string at the same time.
0
 
Ray PaseurCommented:
Lower case and underscores seems to be very popular.  The PHP variable name rules are quite lax.
http://www.php.net/manual/en/language.variables.basics.php

Variable names are case-sensitive.  Class and function names are not.
0
 
Dave BaldwinFixer of ProblemsCommented:
I have always worked as a 'contractor' and not an 'employee' when I've doing PHP.  The only coding I've done as an employee was using assembly language and Borland C and my employer had no idea what I was doing.  So there has never been anyone to tell me what 'naming standard' to use.  Thanks for the points.
0
 
elepilAuthor Commented:
Dave Baldwin,

Borland C? Haha! Really? That platform died ages ago, did it not?

Anyway, it's good to hear that name again, brings back memories.
0
 
Dave BaldwinFixer of ProblemsCommented:
I think that Delphi is the current version of that but I was talking about 1995 at the latest.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now