Solved

Simple question about PHP variable-naming conventions

Posted on 2014-01-13
15
362 Views
Last Modified: 2014-01-13
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
Comment
Question by:elepil
  • 5
  • 4
  • 3
  • +3
15 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 100 total points
Comment Utility
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
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 total points
Comment Utility
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
 

Author Comment

by:elepil
Comment Utility
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
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 100 total points
Comment Utility
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
 

Author Comment

by:elepil
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 58

Expert Comment

by:Gary
Comment Utility
Personally don't like camelCase but that's probably just because of my eyesight!
0
 

Author Comment

by:elepil
Comment Utility
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
 

Author Comment

by:elepil
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 100 total points
Comment Utility
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
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
 

Author Comment

by:elepil
Comment Utility
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
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I think that Delphi is the current version of that but I was talking about 1995 at the latest.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now