3

Is it recommended to write code in uppercase ?

Like:

$VARIABLE = "JohnDoe";

REQUIRE_ONCE 'class.username.php';

$_USERNAME = NEW USERNAME();
$_USERNAME->ADD($VARIABLE);

Thanks.

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
Laurie Cos
  • 75
  • 2
  • 8

2 Answers2

5

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:

true
TRUE
false
FALSE
null
NULL
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
2

In this sort of case.. It just goes down to what you feel comfortable with, bare in mind bad practices in terms of. Some programmers prefer the CamelCase way of writing, others prefer underscore.Personally, I have the habit of doing both depending on the situation..

$theString = "This is a camelcase example";

But you have a preferred style of:

$the_String = "This is not a camelcase example";

In a situation like this, it would be best to keep to camelcase.

Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
  • 1
    Camel Case starting with an upper case character is often called Pascal Case, and sometimes not considered Camel Case at all. Camel Case may be written exclusively as `theString` and not `TheString`. – Magnus Bull Nov 27 '18 at 10:23