Tag Archive | "php variables"

Tags: , , , ,

How to use Variables properly in PHP

Posted on 03 September 2011 by Moki

Variables are used to store information to be used later in your scripts. Variables store values such as text strings, arrays and numbers. When you declare a variable you can use it as many times as you need within your script.

Variables are declared by starting them with a $. For exmple:

$variable = variables value;


$dogsname = “Mofiki”;


Variables containing a string of text must have surrounding “” around the value. If you are saving a number to a variable the quotes are not needed. For example:


<html> <body> <?php $dogsname = "Mofiki"; $dogsage = 5; ?> </body> </html> 


In PHP as you notice above variables can be created anywhere without being pre defined. PHP automatically converts the variable to the data type that corrisponds with the data saved to it. There are a few naming rules for variables listed below:

  • variable names can only start with underscores or letters
  • variables may not use spaces, replace them with underscores
  • variables can only consist of letters, numbers, and underscores

Comments (0)