Avatar of elepil
elepil
 asked on

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.
PHP

Avatar of undefined
Last Comment
Dave Baldwin

8/22/2022 - Mon
SOLUTION
Gary

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Dave Baldwin

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
elepil

ASKER
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?
SOLUTION
Cornelia Yoder

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
elepil

ASKER
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.
Gary

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Gary

Personally don't like camelCase but that's probably just because of my eyesight!
elepil

ASKER
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.
elepil

ASKER
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'.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Gary

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.
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dave Baldwin

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.
elepil

ASKER
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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Dave Baldwin

I think that Delphi is the current version of that but I was talking about 1995 at the latest.