Open Directory Site PHP Tutorial

Home |  ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

 PHP Tutorial >> Variables

PHP - Variables

If you have never had any programming, Algebra, or scripting experience, then the concept of variables will be a new to you.

A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be reused throughout your code, instead of having to type out the actual value, over and over again.
In PHP you define a variable with the following form:

  • $variable_name = Value;

If you forget that dollar sign at the beginning, it will not work. This is a common mistake for new PHP programmers!

A Quick Variable Example

Say that we wanted to store the values that we talked about in the above paragraph. How would we go about doing this? We would first want to make a variable name and then set that equal to the value we want. See our example below for the correct way to do this.

PHPCode:

<?php
$hello = "Hello World!";
$a_number = 4;
?>

Note for programmers: PHP does not require variables to be declared before being initialized.

PHP Variable Naming Conventions

There are a few rules that you need to follow when choosing a name for your PHP variables.

  • PHP variables must start with a letter.

  • PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .

  • Variables with more than one word should be separated with underscores. $my_variable

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting