Anonymous Anonymous - 2 years ago 276
PHP Question

Uppercase for variable on PHP, good or not?

Is it recommended to write code in uppercase ?


$VARIABLE = "JohnDoe";

REQUIRE_ONCE 'class.username.php';



Answer Source

You are free to use whatever case you want.

Historically there have been several conventions. Some preferred uppercase, some lowercase, and yet some who preferred mixed case. Which you used usually depended on how you wrote your HTML, and what background you came from (some programming languages encouraged/required you to use uppercase, for instance, and because PHP is not picky about what case you use it was easy to just continue using what you knew).

Now-a-days there are a couple conventions. The following are generally what you will see when reading other people's code:

Most people use lowercase for variable and function names:

$name = "Some Name";
function age(Person $person) {
    return $person->age;

As for names that contain more than one word there tends to be roughly two camps: Those who prefer camelCase, and those who prefer under_score:

$personName = "Some Name";
function getAge(Person $person) {
    return $person->age;
// Or...
$person_name = "Some Name";
function get_age(Person $person) {
    return $person->age;

For classes/interfaces/etc. most people will use the same convention as variables and functions, but uppercasing the first character. This is probably done in order to make it easier to differentiate them from other things like function names (i.e. functions start with lowercase, classes/interfaces/etc. start with uppercase). Multiple words are usually in CamelCase or in Under_Score.

Class names are often formatted in such a way to facilitate autoloading. Using underscores is a way to easily mirror the underlying folder structure (_ becomes /, i.e. Under/Score). CamelCase can also be used in the same manner by putting the folder separator before the second, and later, capitalised letters (i.e. Camel/Case).

With namespaces becoming more and more popular making the class name analogous to the file path has become less and less necessary (the namespace itself has largely taken on that role). Because it makes a certain kind of sense, and because the autoloading standards/conventions are still around, you will still see it, though.

class Person {}
class ComputerProgrammer extends Person {}
// Or...
class Computer_Programmer extends Person {}

For constants most people will use all UPPERCASE. This is to make constants look distinct from variables/function/etc. It is a convention that comes from the C language, which PHP is closely related to.

define("DB_PASS", "Pa5sW0Rd");

Related to constants, language constant such as booleans and null are usually in either all lowercase or all uppercase:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download