w3b-Doctor - Clean Simple Web Design

Welcome to w3b-Doctor.com The home of clean simple web design.

What is a Variable

If you are looking at this blog, you are most likely new to the world of programming. Welcome to a long yet rewarding journey that will allow you to do many cool things with your computer. This blog will focus on programming in php, but the basic definitions are applicable across most C based language.

So now to the actual reason for writing the blog post. A variable is a named container that can hold pieces of information or values that you can then refer to by that name later in a script. An example is shown below:


// Sets a value of 1 to a variable called $myValue
$myValue = 1;

Important things to note with variable definition in php:

  • Always start a variable with a $ sign
  • Variable names are case sensitive, meaning $helloworld and $helloWorld are different variables.
  • A valid variable name will start with either a letter or underscore, followed by any number of letters, numbers or underscores

More generally it is a good idea to:

  • Keep a consistent naming convention for your variables. For example you will see probably see three different styles of variable naming:
    • All lower case e.g. $myvariablename
    • Camel case e.g. $myVariableName
    • Underscore separated e.g. $my_variable_name
    It doesn't really matter which one you choose, the only important thing is to keep it consistent throughout your scripts. When I started coding, I used the underscore method, over time however I moved to the camel case method. Neither is better it is simply down to which one you prefer.
  • Try to use descriptive names as they are much easier when you have to go back to understand a script in the future, I guarantee you won't remember it 2 years later. So with that in mind $firstName would be more helpful than $name01 which is better than $value01 which is better than $_01

If you are new to programming you will probably be asking yourself why do i need that, can’t I just use the information, why do I need to go to the extra time of defining my values into variables. In basic scripts there is no real benefit except getting into the habit of using them. However, as your scripts get more complicated there are several benefits:

  • You can run the same code with different initialisation values
  • It is often easier to reference things by well defined name that by value. For example you may need to use π in your script. Instead of having to type 3.1415926535.... every time you want to use it you could define it as $pieValue and then only need to use the variable name instead of a long value.
  • Reduces the chance of typo’s. Again using the π example, with a nature of π value it is easy to mistype a digit, so only having to do this once is a benefit.
  • When debugging a script php’s runtime compiler will throw an error for a mistyped variable name, it will not for a mistyped value. So you are getting an extra level of error checking by using Variables.
  • When debugging the script it is easier to read / understand well defined variable names.

© Copyright 2013

w3b-Doctor Blog Terms And Conditions Of Use

All content provided on the w3b-Doctor.com blog is for informational purposes only. We makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site.

The owner of w3b-Doctor.com will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.

This terms and conditions is subject to change at anytime with or without notice.