?
Solved

Simple question about PHP variable-naming conventions

Posted on 2014-01-13
15
Medium Priority
?
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +3
15 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 400 total points
ID: 39778009
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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 400 total points
ID: 39778031
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
ID: 39778052
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.

 
LVL 27

Assisted Solution

by:Cornelia Yoder
Cornelia Yoder earned 400 total points
ID: 39778053
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
ID: 39778062
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
ID: 39778069
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 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 400 total points
ID: 39778094
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
 
LVL 58

Expert Comment

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

Author Comment

by:elepil
ID: 39778105
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
ID: 39778108
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
ID: 39778116
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 111

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 39778117
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 84

Expert Comment

by:Dave Baldwin
ID: 39778126
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
ID: 39778131
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 84

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

752 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