Back to Articles

Variables

Posted in By On 22nd Sep 2009 :: Comments [0]

Variable what?
A variable is something that is used in a formula or algebra. For example, x = 2. What does that make this equation? 3x + 2 = ?  In the example it's 8. The same thing applies in PHP or any kind of coding work. It's a pre-defined bit of information that you can use in the rest of your code.

So how do you define a variable?
PHP recognises $variable_name as a variable. It has to have the dollar sign there to make it a variable. But this example has no value.. Here's how it would look...

$variable_name = "hi im the value of the variable";

Now, wherever you put $variable_name in your code, the text "hi im the value of the variable". Of course you would have to echo or print the variable to make this text display on a page.

  • Variables are CASE SENTIVIVE! PHP will see $variable and $Variable as two completley different variables.
  • A-Z a-z 0-9 _ may all be used in a variable as long as it starts with a letter or underscore.
  • Remember no spaces!!
  • anything can be put in the value of the variable BUT.. putting a " in the middle will cancel out the end " so it will be useless.. put a \" if you want to use it in the middle of a value for a variable
  • Be careful what value you put in a variable.. if you put a text value, and then go on to use it in an addition, then it wont work.. I warned you! ;)

 

Comments - (You must be logged in to add a comment)
1 of 1