Solved

Simple question about PHP variable-naming conventions

Posted on 2014-01-13
15
384 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 100 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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 100 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 27

Assisted Solution

by:Cornelia Yoder
Cornelia Yoder earned 100 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 100 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 110

Accepted Solution

by:
Ray Paseur earned 100 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 83

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 83

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

687 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