Launching Soon
PHP Intro
PHP Syntax
PHP Variables
PHP Echo
PHP String
PHP Operators
PHP If...Else
PHP Switch
PHP Arrays
PHP While Loops
PHP For Loops
PHP Functions
PHP Forms
PHP Advanced
PHP Date
PHP Include
PHP File
PHP File Create
PHP File Open
PHP File Close
PHP File Write
PHP File Read
PHP File Delete
PHP File Append
PHP File Truncate
PHP File Upload
PHP Cookies
PHP Sesssions
PHP Email
PHP Secure Email
PHP Error
PHP Exception
PHP Filter
PHP Database
MY SQL Introduction
MY SQL Connect
MY SQL Create
MY SQL Insert
MY SQL Where
MY SQLOrder By
MY SQL Update
MY SQL Delete
PHP Array
PHP Calender
PHP Date
PHP Directory
PHP Error
PHP File System
PHP Filter
PHP Libxml
PHP Mail
PHP Misc

PHP Mysql

PHP String

php - variables

If you have never had any programming, Algebra, or scripting experience, then the concept of variables might be a new concept to you. A detailed explanation of variables is beyond the scope of this tutorial, but we've included a refresher crash course to guide 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!

Note: Also, variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_numberare different variables in PHP's eyes.

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.

PHP Code:

$hello = "Hello World!";
$a_number = 4;
$anotherNumber = 8;

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 or underscore "_".
  • 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
  • Variables with more than one word can also be distinguished with capitalization. $myVariable



Designed and Developed by MGEF
PHP MGEF Tutorial is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2010 by MGEF. All Rights Reserved.