TUTORIAL: Naming Variables

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Naming Variables - Introduction


This is a small and basic tutorials on how to name your variables... what you can, and what you can't. This could be important.

Please take note, that these rules that apply to naming your variables could very well apply to other naming rules such as naming functions and classes (covered in a later tutorial).

What to do/Not do


Names of variables, functions and classes are to start with a letter... any letter from a to z or A to Z or an underline but no numbers and definately no signs. Below is a list of example names that are correctly named and incorrectly names.

  • $Al34n_3
    • Correct
  • $_3fg
    • Correct
  • $3fv_f
    • Incorrect
  • $fg_#t
    • Incorrect
  • function _()
    • Correct (Yet, I don't recommend it)

The only characters allowed in variable, function and class names are any letters (lowercase or uppercase) or and underline followed by any numbers. Of course you could only have letters or letters mixed with numbers or underlines.

Even though you can have a function named _, I don't recommend it... why? Well, this brings us to the next section.

Naming Variables, functions, classes


The reason I don't recommend you to name a function simply by an underline (_) is because it doesn't describe the function any. I recommend you that you name the variable, function, or class in a way that describes what it's holding in one or two words... that is where the underline comes in.

Lets say that you have a variable holding someone's name... you wouldn't name it $city would you? (Unless it's a city's name), you would name it either $persons_name or simply $person...

Conclusion


Well, I hope that you learned the correct way to name your variables, functions, and classes correctly in this short tutorial. Take care :)
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Just in addition, if you had the variable name $person it would be related back to an instance of a person object (the same with $city)

This is the ideal situation, using objects. so $person->getName() would return their name. if you have assigned the name value to the variable rather be a bit more verbose so instead of $person use $personName or $cityName.

The use of underscores is also frowned upon as it is an additional key stroke. Camel notation is the prefered method now a days. Constants generally have underscores in them as they are generally in upper case and MYSUPERCONSTANT doesn't make as much sense as MY_SUPER_CONSTANT (or maybe it does).

Just my 2 cents :)

Post Information

  • Total Posts in this topic: 2 posts
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 3 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.